Spring Data JPA

org.springframework.data.jpa.repository.support
Class SimpleJpaRepository<T,ID extends Serializable>

java.lang.Object
  extended by org.springframework.data.jpa.repository.support.SimpleJpaRepository<T,ID>
Type Parameters:
T - the type of the entity to handle
ID - the type of the entity's identifier
All Implemented Interfaces:
JpaRepository<T,ID>, JpaSpecificationExecutor<T>, CrudRepository<T,ID>, PagingAndSortingRepository<T,ID>, Repository<T,ID>
Direct Known Subclasses:
QueryDslJpaRepository

@Repository
@Transactional(readOnly=true)
public class SimpleJpaRepository<T,ID extends Serializable>
extends Object
implements JpaRepository<T,ID>, JpaSpecificationExecutor<T>

Default implementation of the CrudRepository interface. This will offer you a more sophisticated interface than the plain EntityManager .

Author:
Oliver Gierke, Eberhard Wolff, Thomas Darimont

Constructor Summary
SimpleJpaRepository(Class<T> domainClass, javax.persistence.EntityManager em)
          Creates a new SimpleJpaRepository to manage objects of the given domain type.
SimpleJpaRepository(JpaEntityInformation<T,?> entityInformation, javax.persistence.EntityManager entityManager)
          Creates a new SimpleJpaRepository to manage objects of the given JpaEntityInformation.
 
Method Summary
 long count()
           
 long count(Specification<T> spec)
          Returns the number of instances that the given Specification will return.
 void delete(ID id)
           
 void delete(Iterable<? extends T> entities)
           
 void delete(T entity)
           
 void deleteAll()
           
 void deleteAllInBatch()
          Deletes all entites in a batch call.
 void deleteInBatch(Iterable<T> entities)
          Deletes the given entities in a batch which means it will create a single Query.
 boolean exists(ID id)
           
 List<T> findAll()
           
 List<T> findAll(Iterable<ID> ids)
           
 Page<T> findAll(Pageable pageable)
           
 List<T> findAll(Sort sort)
           
 List<T> findAll(Specification<T> spec)
          Returns all entities matching the given Specification.
 Page<T> findAll(Specification<T> spec, Pageable pageable)
          Returns a Page of entities matching the given Specification.
 List<T> findAll(Specification<T> spec, Sort sort)
          Returns all entities matching the given Specification and Sort.
 T findOne(ID id)
           
 T findOne(Specification<T> spec)
          Returns a single entity matching the given Specification.
 void flush()
          Flushes all pending changes to the database.
protected  javax.persistence.TypedQuery<Long> getCountQuery(Specification<T> spec)
          Creates a new count query for the given Specification.
protected  Class<T> getDomainClass()
           
 T getOne(ID id)
          Returns a reference to the entity with the given identifier.
protected  javax.persistence.TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable)
          Creates a new TypedQuery from the given Specification.
protected  javax.persistence.TypedQuery<T> getQuery(Specification<T> spec, Sort sort)
          Creates a TypedQuery for the given Specification and Sort.
protected  Page<T> readPage(javax.persistence.TypedQuery<T> query, Pageable pageable, Specification<T> spec)
          Reads the given TypedQuery into a Page applying the given Pageable and Specification.
<S extends T>
List<S>
save(Iterable<S> entities)
           
<S extends T>
S
save(S entity)
           
<S extends T>
S
saveAndFlush(S entity)
          Saves an entity and flushes changes instantly.
 void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata)
          Configures a custom CrudMethodMetadata to be used to detect LockModeTypes and query hints to be applied to queries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleJpaRepository

public SimpleJpaRepository(JpaEntityInformation<T,?> entityInformation,
                           javax.persistence.EntityManager entityManager)
Creates a new SimpleJpaRepository to manage objects of the given JpaEntityInformation.

Parameters:
entityInformation - must not be null.
entityManager - must not be null.

SimpleJpaRepository

public SimpleJpaRepository(Class<T> domainClass,
                           javax.persistence.EntityManager em)
Creates a new SimpleJpaRepository to manage objects of the given domain type.

Parameters:
domainClass - must not be null.
em - must not be null.
Method Detail

setRepositoryMethodMetadata

public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata)
Configures a custom CrudMethodMetadata to be used to detect LockModeTypes and query hints to be applied to queries.

Parameters:
crudMethodMetadata -

getDomainClass

protected Class<T> getDomainClass()

delete

@Transactional
public void delete(ID id)
Specified by:
delete in interface CrudRepository<T,ID extends Serializable>

delete

@Transactional
public void delete(T entity)
Specified by:
delete in interface CrudRepository<T,ID extends Serializable>

delete

@Transactional
public void delete(Iterable<? extends T> entities)
Specified by:
delete in interface CrudRepository<T,ID extends Serializable>

deleteInBatch

@Transactional
public void deleteInBatch(Iterable<T> entities)
Description copied from interface: JpaRepository
Deletes the given entities in a batch which means it will create a single Query. Assume that we will clear the EntityManager after the call.

Specified by:
deleteInBatch in interface JpaRepository<T,ID extends Serializable>

deleteAll

@Transactional
public void deleteAll()
Specified by:
deleteAll in interface CrudRepository<T,ID extends Serializable>

deleteAllInBatch

@Transactional
public void deleteAllInBatch()
Description copied from interface: JpaRepository
Deletes all entites in a batch call.

Specified by:
deleteAllInBatch in interface JpaRepository<T,ID extends Serializable>

findOne

public T findOne(ID id)
Specified by:
findOne in interface CrudRepository<T,ID extends Serializable>

getOne

public T getOne(ID id)
Description copied from interface: JpaRepository
Returns a reference to the entity with the given identifier.

Specified by:
getOne in interface JpaRepository<T,ID extends Serializable>
Parameters:
id - must not be null.
Returns:
a reference to the entity with the given identifier.
See Also:
EntityManager.getReference(Class, Object)

exists

public boolean exists(ID id)
Specified by:
exists in interface CrudRepository<T,ID extends Serializable>

findAll

public List<T> findAll()
Specified by:
findAll in interface JpaRepository<T,ID extends Serializable>
Specified by:
findAll in interface CrudRepository<T,ID extends Serializable>

findAll

public List<T> findAll(Iterable<ID> ids)
Specified by:
findAll in interface JpaRepository<T,ID extends Serializable>
Specified by:
findAll in interface CrudRepository<T,ID extends Serializable>

findAll

public List<T> findAll(Sort sort)
Specified by:
findAll in interface JpaRepository<T,ID extends Serializable>
Specified by:
findAll in interface PagingAndSortingRepository<T,ID extends Serializable>

findAll

public Page<T> findAll(Pageable pageable)
Specified by:
findAll in interface PagingAndSortingRepository<T,ID extends Serializable>

findOne

public T findOne(Specification<T> spec)
Description copied from interface: JpaSpecificationExecutor
Returns a single entity matching the given Specification.

Specified by:
findOne in interface JpaSpecificationExecutor<T>
Returns:

findAll

public List<T> findAll(Specification<T> spec)
Description copied from interface: JpaSpecificationExecutor
Returns all entities matching the given Specification.

Specified by:
findAll in interface JpaSpecificationExecutor<T>
Returns:

findAll

public Page<T> findAll(Specification<T> spec,
                       Pageable pageable)
Description copied from interface: JpaSpecificationExecutor
Returns a Page of entities matching the given Specification.

Specified by:
findAll in interface JpaSpecificationExecutor<T>
Returns:

findAll

public List<T> findAll(Specification<T> spec,
                       Sort sort)
Description copied from interface: JpaSpecificationExecutor
Returns all entities matching the given Specification and Sort.

Specified by:
findAll in interface JpaSpecificationExecutor<T>
Returns:

count

public long count()
Specified by:
count in interface CrudRepository<T,ID extends Serializable>

count

public long count(Specification<T> spec)
Description copied from interface: JpaSpecificationExecutor
Returns the number of instances that the given Specification will return.

Specified by:
count in interface JpaSpecificationExecutor<T>
Parameters:
spec - the Specification to count instances for
Returns:
the number of instances

save

@Transactional
public <S extends T> S save(S entity)
Specified by:
save in interface CrudRepository<T,ID extends Serializable>

saveAndFlush

@Transactional
public <S extends T> S saveAndFlush(S entity)
Description copied from interface: JpaRepository
Saves an entity and flushes changes instantly.

Specified by:
saveAndFlush in interface JpaRepository<T,ID extends Serializable>
Returns:
the saved entity

save

@Transactional
public <S extends T> List<S> save(Iterable<S> entities)
Specified by:
save in interface JpaRepository<T,ID extends Serializable>
Specified by:
save in interface CrudRepository<T,ID extends Serializable>

flush

@Transactional
public void flush()
Description copied from interface: JpaRepository
Flushes all pending changes to the database.

Specified by:
flush in interface JpaRepository<T,ID extends Serializable>

readPage

protected Page<T> readPage(javax.persistence.TypedQuery<T> query,
                           Pageable pageable,
                           Specification<T> spec)
Reads the given TypedQuery into a Page applying the given Pageable and Specification.

Parameters:
query - must not be null.
spec - can be null.
pageable - can be null.
Returns:

getQuery

protected javax.persistence.TypedQuery<T> getQuery(Specification<T> spec,
                                                   Pageable pageable)
Creates a new TypedQuery from the given Specification.

Parameters:
spec - can be null.
pageable - can be null.
Returns:

getQuery

protected javax.persistence.TypedQuery<T> getQuery(Specification<T> spec,
                                                   Sort sort)
Creates a TypedQuery for the given Specification and Sort.

Parameters:
spec - can be null.
sort - can be null.
Returns:

getCountQuery

protected javax.persistence.TypedQuery<Long> getCountQuery(Specification<T> spec)
Creates a new count query for the given Specification.

Parameters:
spec - can be null.
Returns:

Spring Data JPA

Copyright © 2011-2014–2014 Pivotal Software, Inc.. All rights reserved.