intra-mart Accel Platform / TERASOLUNA Global Framework Programming Guide

Version 2 2014-01-01

Contents

Programming Method using Spring Data JPA

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 defining
the 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.

Shared Database Usage

Following settings are necessary to connect to the shared database.

Warning

In case shared database is used, package for the repository class and package for the entity class should be separately managed for each of tenant and shared.
  1. Please remove the comment-out of sharedDataSource of applicationContext-im_tgfw_common.xml.
    Here, connection ID of the subject shared database should be specified to the connectId.
<bean id="dataSource"   class="jp.co.intra_mart.framework.extension.spring.datasource.TenantDataSource" />

<bean id="sharedDataSource" class="jp.co.intra_mart.framework.extension.spring.datasource.SharedDataSource">
  <constructor-arg name="connectId" value="sharedA" />
</bean>
  1. Please remove the comment-out of sharedEntityManagerFactory of applicationContext-im_tgfw_jpa.xml.
    Here, the package of entity class of shared database should be specified to the packagesToScan.
<bean id="sharedEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="shared" />
  <property name="packagesToScan" value="sample.spring.domain.model.shared" />
  <property name="jtaDataSource" ref="sharedDataSource" />
  <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
  <property name="jpaPropertyMap" ref="hibernateProperties" />
</bean>
  1. Package of repository to be scanned by Spring Data JPA will be defined for each database as follows.
    Here, it is necessary to explicitly specify the entity-manager-factory-ref attribute.
    <jpa:repositories base-package="sample.spring.domain.repository.tenant" entity-manager-factory-ref="entityManagerFactory" />
    <jpa:repositories base-package="sample.spring.domain.repository.shared" entity-manager-factory-ref="sharedEntityManagerFactory" />
    

Note

When you select [TERASOLUNA Global Framework for JPA Setting File] from [Select Additional Resource] of Juggling project,
setting file which includes the file above will be placed on the Juggling project.
If you place the edit file above on the module project, it may possibly affect the other modules.
Therefore, the files shown as above must be always edited/managed on the Juggling project, and shoould not be placed on the module project.

Contents