添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
捣蛋的手术刀  ·  wellerman由来_百度知道·  3 月前    · 
干练的煎饼果子  ·  linux - Ansible ...·  1 年前    · 
忐忑的豆芽  ·  BuildProperties - ...·  1 年前    · 

The JPA module of Spring Data contains a custom namespace that allows defining repository beans. It also contains certain features and element attributes that are special to JPA. Generally the JPA repositories can be set up using the repositories element:


Using this element looks up Spring Data repositories as described in Section 1.2.3, “Creating repository instances” . Beyond that it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring's DataAccessException hierarchy.

The Spring Data JPA repositories support cannot only be activated through an XML namespace but also using an annotation through JavaConfig.


The just shown configuration class sets up an embedded HSQL database using the EmbeddedDatabaseBuilder API of spring-jdbc. We then set up a EntityManagerFactory and use Hibernate as sample persistence provider. The last infrastructure component declared here is the JpaTransactionManager . We eventually activate Spring Data JPA repositories using the @EnableJpaRepositories annotation which essentially carries the same attributes as the XML namespace does. If no base package is configured it will use the one the configuration class resides in.

Generally the query creation mechanism for JPA works as described in Section 1.2, “Query methods” . Here's a short example of what a JPA query method translates into:


Table 2.3. Supported keywords inside method names

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = 1?
Between findByStartDateBetween … where x.startDate between 1? and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended % )
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended % )
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in % )
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> age) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)


[Note] Note

In and NotIn also take any subclass of Collection as parameter as well as arrays or varargs. For other syntactical versions of the very same logical operator check Appendix B, Repository query keywords .

[Note] Note

The examples use simple <named-query /> element and @NamedQuery annotation. The queries for these configuration elements have to be defined in JPA query language. Of course you can use <named-native-query /> or @NamedNativeQuery too. These elements allow you to define the query in native SQL by losing the database platform independence.

Using named queries to declare queries for entities is a valid approach and works fine for a small number of queries. As the queries themselves are tied to the Java method that executes them you actually can bind them directly using the Spring Data JPA @Query annotation rather than annotating them to the domain class. This will free the domain class from persistence specific information and co-locate the query to the repository interface.

Queries annotated to the query method will take precedence over queries defined using @NamedQuery or named queries declared in orm.xml .


As of Spring Data JPA Release 1.4 we support the usage of restricted SpEL template expressions in manually defined queries via @Query . Upon query execution these expressions are evaluated against a predefined set of variables. We support the following list of variables to be used in a manual query.


The following example demonstrates one use case for the #{#entityName} expression in a query string where you want to define a repository interface with a query method with a manually defined query. In order not to have to state the actual entity name in the query string of a @Query annotation one can use the #{#entityName} Variable.


Of course you could have just used User in the query declaration directly but that would require you to change the query as well. The reference to #entityName will pick up potential future remappings of the User class to a different entity name (e.g. by using @Entity(name = "MyUser") .

Another use case for the #{#entityName} expression in a query string is if you want to define a generic repository interface with specialized repository interfaces for a concrete domain type. In order not to have to repeat the definition of custom query methods on the concrete interfaces you can use the entity name expression in the query string of the @Query annotation in the generic repository interface.


In the example the interface MappedTypeRepository is the common parent interface for a few domain types extending AbstractMappedType . It also defines the generic method findAllByAttribute(…) which can be used on instances of the specialized repository interfaces. If you now invoke findByAllAttribute(…) on ConcreteRepository the query being executed will be select t from ConcreteType t where t.attribute = ?1 .

JPA 2 introduces a criteria API that can be used to build queries programmatically. Writing a criteria you actually define the where-clause of a query for a domain class. Taking another step back these criteria can be regarded as predicate over the entity that is described by the JPA criteria API constraints.

Spring Data JPA takes the concept of a specification from Eric Evans' book "Domain Driven Design", following the same semantics and providing an API to define such Specification s using the JPA criteria API. To support specifications you can extend your repository interface with the JpaSpecificationExecutor interface:

public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
}

The additional interface carries methods that allow you to execute Specification s in a variety of ways.

For example, the findAll method will return all entities that match the specification:

List<T> findAll(Specification<T> spec);

The Specification interface is as follows:

public interface Specification<T> {
  Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
            CriteriaBuilder builder);
}

Okay, so what is the typical use case? Specification s can easily be used to build an extensible set of predicates on top of an entity that then can be combined and used with JpaRepository without the need to declare a query (method) for every needed combination. Here's an example:


Admittedly the amount of boilerplate leaves room for improvement (that will hopefully be reduced by Java 8 closures) but the client side becomes much nicer as you will see below. The Customer_ type is a metamodel type generated using the JPA Metamodel generator (see the Hibernate implementation's documentation for example ). So the expression Customer_.createdAt is asuming the Customer having a createdAt attribute of type Date . Besides that we have expressed some criteria on a business requirement abstraction level and created executable Specification s. So a client might use a Specification as follows:


Okay, why not simply create a query for this kind of data access? You're right. Using a single Specification does not gain a lot of benefit over a plain query declaration. The power of Specification s really shines when you combine them to create new Specification objects. You can achieve this through the Specifications helper class we provide to build expressions like this:


CRUD methods on repository instances are transactional by default. For reading operations the transaction configuration readOnly flag is set to true, all others are configured with a plain @Transactional so that default transaction configuration applies. For details see JavaDoc of Repository . If you need to tweak transaction configuration for one of the methods declared in Repository simply redeclare the method in your repository interface as follows:


Another possibility to alter transactional behaviour is using a facade or service implementation that typically covers more than one repository. Its purpose is to define transactional boundaries for non-CRUD operations:


Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and the point in time this happened. To benefit from that functionality you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface.

Spring Data JPA ships with an entity listener that can be used to trigger capturing auditing information. So first you have to register the AuditingEntityListener inside your orm.xml to be used for all entities in your persistence contexts:

Note that the auditing feature requires spring-aspects.jar to be on the classpath.


Now activating auditing functionality is just a matter of adding the Spring Data JPA auditing namespace element to your configuration:


As of Spring Data JPA 1.5, auditing can be enabled by annotating a configuration class with the @EnableJpaAuditing annotation.


If you expose a bean of type AuditorAware to the ApplicationContext , the auditing infrastructure will pick it up automatically and use it to determine the current user to be set on domain types. If you have multiple implementations registered in the ApplicationContext , you can select the one to be used by explicitly setting the auditorAwareRef attribute of @EnableJpaAuditing .

Instances of the repository interfaces are usually created by a container, which Spring is the most natural choice when working with Spring Data. There's sophisticated support to easily set up Spring to create bean instances documented in Section 1.2.3, “Creating repository instances” . As of version 1.1.0 Spring Data JPA ships with a custom CDI extension that allows using the repository abstraction in CDI environments. The extension is part of the JAR so all you need to do to activate it is dropping the Spring Data JPA JAR into your classpath.

You can now set up the infrastructure by implementing a CDI Producer for the EntityManagerFactory and EntityManager :

class EntityManagerFactoryProducer {
  @Produces
  @ApplicationScoped
  public EntityManagerFactory createEntityManagerFactory() {
    return Persistence.createEntityManagerFactory("my-presistence-unit");
  public void close(@Disposes EntityManagerFactory entityManagerFactory) {
    entityManagerFactory.close();
  @Produces
  @RequestScoped
  public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) {
    return entityManagerFactory.createEntityManager();
  public void close(@Disposes EntityManager entityManager) {
    entityManager.close();
}

The necessary setup can vary depending on the JavaEE environment you run in. It might also just be enough to redeclare a EntityManager as CDI bean as follows:

class CdiConfig {
  @Produces
  @RequestScoped
  @PersistenceContext
  public EntityManager entityManager;
}

In this example, the container has to be capable of creating JPA EntityManager s itself. All the configuration does is re-exporting the JPA EntityManager as CDI bean.

The Spring Data JPA CDI extension will pick up all EntityManager s availables as CDI beans and create a proxy for a Spring Data repository whenever an bean of a repository type is requested by the container. Thus obtaining an instance of a Spring Data repository is a matter of declaring an @Inject ed property:

class RepositoryClient {
  @Inject
  PersonRepository repository;