Programming Method using Spring Data JPA¶
Topics
Summary¶
One example of programming method using Spring Data JPA is shown in this section.Please see below for such detail information as specifications.
About Spring Data JPA¶
JPA(Java Persistence API) is a specification about persistence,and Spring Data JPA is an O/R mapping framework that supports it.When you use Spring Data JPA, you can obtain the Entity that matches with the condition by simply definingthe Query method to the Repository interface. Therefore, you can reduce the implementation needed to operate the Entity.
Setting¶
In order to use the JPA function, it is necessary to set the entity class as the subject for management of JPA in advance.Please open applicationContext-im_tgfw_jpa.xml, and set the value attributes at the locations below to the root package of entity class.<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan" value="my.terasoluna.app.domain.entity" /> <property name="jtaDataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="jpaPropertyMap" ref="hibernateProperties" /> <property name="persistenceUnitName" value="tenant" /> </bean>Note
applicationContext-im_tgfw_jpa.xml is a setting file common in the tenant. It is usually edited by Juggling project. In case you need to register the root package of multiple entity classes, please use commas as delimiters.Note
For the creation method of entity, please refer to Entity Creation from Eclipse.It is also necessary to specify the root package of repository class to the arbitrary appliationContext.xml in the module project as shown below.<jpa:repositories base-package="my.terasoluna.app.domain.repository" />
Repository Class Creation¶
In case JpaRepository is used for the implementation, interface that inherits JpaRepository should be defined as follows.Entity class is specified as the first argument, and the field type of main key is specified as the second argument of generics.package my.terasoluna.app.domain.repository; import my.terasoluna.app.domain.model.MyCompany; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface MyCompanyRepository extends JpaRepository<MyCompany, String> { }Main processes are implemented in JpaRepository in advance. Please refer to the API List below for the interface specifications.In case interface is not used, @RepositoryDefinition is defined as below, and entity is set to domainClass, and main key type is set to idClass.import org.springframework.data.repository.RepositoryDefinition; ... @RepositoryDefinition(domainClass=MyCompany.class, idClass=String.class) public interface MyCompanyRepositoryNoSuper { public void save(MyCompany entity); public void delete(MyCompany entity); public MyCompany findOne(String companyId); ...In this case, please define the method name according to the [Method Signature] stated below.
Query Method¶
Query method is defined as a method of Repository interface for each Entity.Query to be used can be mainly specified by the two types of methods stated below.
Method Signature¶
If the method name is defined by following the particular naming rule,Query(JPQL) which executes from the method name by the Spring Data JPA function is generated.<*> Only the SELECT statement is supported.List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
If the method name as above is defined, JPQL to be executed will be as follows.select u from User u where u.emailAddress = ?1 and u.lastname = ?2
Query Annotation¶
In case JPQL at the time of method execution is explicitly specified,@Query annotation is used as below.@Query("select u from User u where u.emailAddress = ?1") User findByEmailAddress(String emailAddress);Note
Please refer to the page below for the detail information about the query method.
Pagination¶
PageRequest is used to relaize the paging function by Spring Data JPA.import static org.springframework.data.domain.Sort.Direction.ASC; import static org.springframework.data.domain.Sort.Direction.DESC; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; ... public List<MyCompany> fetch(int page, int pageSize) { Pageable pageable = new PageRequest(page, pageSize, ASC, "id"); return myCompanyRepository.findAll(pageable).getContent(); }Note
PageRequestのより詳しい情報については、下記ページを参照してください。