Class SimpleJpaRepository<T,ID>
java.lang.Object
org.springframework.data.jpa.repository.support.SimpleJpaRepository<T,ID>
- Type Parameters:
T- the type of the entity to handleID- the type of the entity's identifier
- All Implemented Interfaces:
JpaRepository<T,,ID> JpaSpecificationExecutor<T>,JpaRepositoryImplementation<T,,ID> org.springframework.data.repository.CrudRepository<T,,ID> org.springframework.data.repository.ListCrudRepository<T,,ID> org.springframework.data.repository.ListPagingAndSortingRepository<T,,ID> org.springframework.data.repository.PagingAndSortingRepository<T,,ID> org.springframework.data.repository.query.QueryByExampleExecutor<T>,org.springframework.data.repository.Repository<T,ID>
- Direct Known Subclasses:
QuerydslJpaRepository
@Repository
@Transactional(readOnly=true)
public class SimpleJpaRepository<T,ID>
extends Object
implements JpaRepositoryImplementation<T,ID>
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, Mark Paluch, Christoph Strobl, Stefan Fussenegger, Jens Schauder, David Madden, Moritz Becker, Sander Krabbenborg, Jesse Wouters, Greg Turnquist, Yanming Zhou, Ernst-Jan van der Laan, Diego Krupitza
-
Constructor Summary
ConstructorsConstructorDescriptionSimpleJpaRepository(Class<T> domainClass, jakarta.persistence.EntityManager entityManager) Creates a newSimpleJpaRepositoryto manage objects of the given domain type.SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, jakarta.persistence.EntityManager entityManager) Creates a newSimpleJpaRepositoryto manage objects of the givenJpaEntityInformation. -
Method Summary
Modifier and TypeMethodDescriptionlongcount()<S extends T>
longcount(org.springframework.data.domain.Example<S> example) longcount(Specification<T> spec) Returns the number of instances that the givenSpecificationwill return.longdelete(Specification<T> spec) Deletes by theSpecificationand returns the number of rows deleted.voidvoidvoidvoiddeleteAllById(Iterable<? extends ID> ids) voiddeleteAllByIdInBatch(Iterable<ID> ids) Deletes the entities identified by the given ids using a single query.voidDeletes all entities in a batch call.voiddeleteAllInBatch(Iterable<T> entities) Deletes the given entities in a batch which means it will create a single query.voiddeleteById(ID id) <S extends T>
booleanexists(org.springframework.data.domain.Example<S> example) booleanexists(Specification<T> spec) Checks whether the data store contains elements that match the givenSpecification.booleanexistsById(ID id) findAll()findAll(org.springframework.data.domain.Example<S> example) <S extends T>
org.springframework.data.domain.Page<S>findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Pageable pageable) findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort) org.springframework.data.domain.Page<T>findAll(org.springframework.data.domain.Pageable pageable) findAll(org.springframework.data.domain.Sort sort) findAll(Specification<T> spec) Returns all entities matching the givenSpecification.org.springframework.data.domain.Page<T>findAll(Specification<T> spec, org.springframework.data.domain.Pageable pageable) Returns aPageof entities matching the givenSpecification.findAll(Specification<T> spec, org.springframework.data.domain.Sort sort) Returns all entities matching the givenSpecificationandSort.findAllById(Iterable<ID> ids) <S extends T,R>
RfindBy(org.springframework.data.domain.Example<S> example, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>, R> queryFunction) <S extends T,R>
RfindBy(Specification<T> spec, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.findOne(org.springframework.data.domain.Example<S> example) findOne(Specification<T> spec) Returns a single entity matching the givenSpecificationorOptional.empty()if none found.voidflush()Flushes all pending changes to the database.Deprecated.getCountQuery(Specification<S> spec, Class<S> domainClass) Creates a new count query for the givenSpecification.protected jakarta.persistence.TypedQuery<Long>getCountQuery(Specification<T> spec) Deprecated.Deprecated.protected <S extends T>
jakarta.persistence.TypedQuery<S>getQuery(Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.protected <S extends T>
jakarta.persistence.TypedQuery<S>getQuery(Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Sort sort) protected jakarta.persistence.TypedQuery<T>getQuery(Specification<T> spec, org.springframework.data.domain.Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.protected jakarta.persistence.TypedQuery<T>getQuery(Specification<T> spec, org.springframework.data.domain.Sort sort) protected QueryHintsReturnsQueryHintswith the query hints based on the currentCrudMethodMetadataand potentialEntityGraphinformation.protected QueryHintsReturnsQueryHintswith the query hints on the currentCrudMethodMetadatafor count queries.getReferenceById(ID id) Returns a reference to the entity with the given identifier.protected CrudMethodMetadataprotected <S extends T>
org.springframework.data.domain.Page<S>readPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, org.springframework.data.domain.Pageable pageable, Specification<S> spec) protected org.springframework.data.domain.Page<T>readPage(jakarta.persistence.TypedQuery<T> query, org.springframework.data.domain.Pageable pageable, Specification<T> spec) Deprecated.<S extends T>
Ssave(S entity) saveAllAndFlush(Iterable<S> entities) Saves all entities and flushes changes instantly.<S extends T>
SsaveAndFlush(S entity) Saves an entity and flushes changes instantly.voidsetEscapeCharacter(EscapeCharacter escapeCharacter) Configures theEscapeCharacterto be used with the repository.voidsetRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) Configures a customCrudMethodMetadatato be used to detectLockModeTypes 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, waitMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteInBatch
-
Constructor Details
-
SimpleJpaRepository
public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, jakarta.persistence.EntityManager entityManager) Creates a newSimpleJpaRepositoryto manage objects of the givenJpaEntityInformation.- Parameters:
entityInformation- must not be null.entityManager- must not be null.
-
SimpleJpaRepository
Creates a newSimpleJpaRepositoryto manage objects of the given domain type.- Parameters:
domainClass- must not be null.entityManager- must not be null.
-
-
Method Details
-
setRepositoryMethodMetadata
Configures a customCrudMethodMetadatato be used to detectLockModeTypes and query hints to be applied to queries.- Specified by:
setRepositoryMethodMetadatain interfaceJpaRepositoryImplementation<T,ID> - Parameters:
crudMethodMetadata-
-
setEscapeCharacter
Description copied from interface:JpaRepositoryImplementationConfigures theEscapeCharacterto be used with the repository.- Specified by:
setEscapeCharacterin interfaceJpaRepositoryImplementation<T,ID> - Parameters:
escapeCharacter- Must not be null.
-
getRepositoryMethodMetadata
-
getDomainClass
-
deleteById
-
delete
-
deleteAllById
-
deleteAllByIdInBatch
Description copied from interface:JpaRepositoryDeletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first level cache and the database out of sync. Consider flushing theEntityManagerbefore calling this method.- Specified by:
deleteAllByIdInBatchin interfaceJpaRepository<T,ID> - Parameters:
ids- the ids of the entities to be deleted. Must not be null.
-
deleteAll
-
deleteAllInBatch
Description copied from interface:JpaRepositoryDeletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs first level cache and the database out of sync. Consider flushing theEntityManagerbefore calling this method.It will also NOT honor cascade semantics of JPA, nor will it emit JPA lifecycle events.
- Specified by:
deleteAllInBatchin interfaceJpaRepository<T,ID> - Parameters:
entities- entities to be deleted. Must not be null.
-
deleteAll
@Transactional public void deleteAll() -
deleteAllInBatch
@Transactional public void deleteAllInBatch()Description copied from interface:JpaRepositoryDeletes all entities in a batch call.- Specified by:
deleteAllInBatchin interfaceJpaRepository<T,ID>
-
findById
-
getOne
Deprecated.Description copied from interface:JpaRepositoryReturns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw anEntityNotFoundExceptionon first access. Some of them will reject invalid identifiers immediately.- Specified by:
getOnein interfaceJpaRepository<T,ID> - Parameters:
id- must not be null.- Returns:
- a reference to the entity with the given identifier.
- See Also:
-
for details on when an exception is thrown.
-
getById
Deprecated.Description copied from interface:JpaRepositoryReturns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw anEntityNotFoundExceptionon first access. Some of them will reject invalid identifiers immediately.- Specified by:
getByIdin interfaceJpaRepository<T,ID> - Parameters:
id- must not be null.- Returns:
- a reference to the entity with the given identifier.
- See Also:
-
for details on when an exception is thrown.
-
getReferenceById
Description copied from interface:JpaRepositoryReturns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw anEntityNotFoundExceptionon first access. Some of them will reject invalid identifiers immediately.- Specified by:
getReferenceByIdin interfaceJpaRepository<T,ID> - Parameters:
id- must not be null.- Returns:
- a reference to the entity with the given identifier.
- See Also:
-
for details on when an exception is thrown.
-
existsById
-
findAll
-
findAllById
-
findAll
-
findAll
public org.springframework.data.domain.Page<T> findAll(org.springframework.data.domain.Pageable pageable) -
findOne
Description copied from interface:JpaSpecificationExecutorReturns a single entity matching the givenSpecificationorOptional.empty()if none found.- Specified by:
findOnein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- must not be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutorReturns all entities matching the givenSpecification.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- must not be null.- Returns:
- never null.
-
findAll
public org.springframework.data.domain.Page<T> findAll(Specification<T> spec, org.springframework.data.domain.Pageable pageable) Description copied from interface:JpaSpecificationExecutorReturns aPageof entities matching the givenSpecification.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- must not be null.pageable- must not be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutorReturns all entities matching the givenSpecificationandSort.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- must not be null.sort- must not be null.- Returns:
- never null.
-
exists
Description copied from interface:JpaSpecificationExecutorChecks whether the data store contains elements that match the givenSpecification.- Specified by:
existsin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theSpecificationto use for the existence check, ust not be null.- Returns:
trueif the data store contains elements that match the givenSpecificationotherwisefalse.
-
delete
Description copied from interface:JpaSpecificationExecutorDeletes by theSpecificationand returns the number of rows deleted.This method uses
Criteria API bulk deletethat maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.Please note that
CriteriaQueryin,Specification.toPredicate(Root, CriteriaQuery, CriteriaBuilder)will be null becauseCriteriaBuilder.createCriteriaDelete(Class)does not implementCriteriaQuery.- Specified by:
deletein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theSpecificationto use for the existence check, must not be null.- Returns:
- the number of entities deleted.
-
findBy
public <S extends T,R> R findBy(Specification<T> spec, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>, R> queryFunction) Description copied from interface:JpaSpecificationExecutorReturns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.- Specified by:
findByin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- must not be null.queryFunction- the query function defining projection, sorting, and the result type- Returns:
- all entities matching the given Example.
-
findOne
- Specified by:
findOnein interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
count
- Specified by:
countin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
exists
- Specified by:
existsin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
findAll
- Specified by:
findAllin interfaceJpaRepository<T,ID> - Specified by:
findAllin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
findAll
public <S extends T> List<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort) - Specified by:
findAllin interfaceJpaRepository<T,ID> - Specified by:
findAllin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
findAll
public <S extends T> org.springframework.data.domain.Page<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Pageable pageable) - Specified by:
findAllin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
findBy
public <S extends T,R> R findBy(org.springframework.data.domain.Example<S> example, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>, R> queryFunction) - Specified by:
findByin interfaceorg.springframework.data.repository.query.QueryByExampleExecutor<T>
-
count
public long count() -
count
Description copied from interface:JpaSpecificationExecutorReturns the number of instances that the givenSpecificationwill return.- Specified by:
countin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theSpecificationto count instances for, must not be null.- Returns:
- the number of instances.
-
save
-
saveAndFlush
Description copied from interface:JpaRepositorySaves an entity and flushes changes instantly.- Specified by:
saveAndFlushin interfaceJpaRepository<T,ID> - Parameters:
entity- entity to be saved. Must not be null.- Returns:
- the saved entity
-
saveAll
-
saveAllAndFlush
Description copied from interface:JpaRepositorySaves all entities and flushes changes instantly.- Specified by:
saveAllAndFlushin interfaceJpaRepository<T,ID> - Parameters:
entities- entities to be saved. Must not be null.- Returns:
- the saved entities
-
flush
@Transactional public void flush()Description copied from interface:JpaRepositoryFlushes all pending changes to the database.- Specified by:
flushin interfaceJpaRepository<T,ID>
-
readPage
@Deprecated protected org.springframework.data.domain.Page<T> readPage(jakarta.persistence.TypedQuery<T> query, org.springframework.data.domain.Pageable pageable, @Nullable Specification<T> spec) Deprecated.- Parameters:
query- must not be null.spec- can be null.pageable- must not be null.
-
readPage
protected <S extends T> org.springframework.data.domain.Page<S> readPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, org.springframework.data.domain.Pageable pageable, @Nullable Specification<S> spec) - Parameters:
query- must not be null.domainClass- must not be null.spec- can be null.pageable- can be null.
-
getQuery
protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, org.springframework.data.domain.Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.- Parameters:
spec- can be null.pageable- must not be null.
-
getQuery
protected <S extends T> jakarta.persistence.TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.- Parameters:
spec- can be null.domainClass- must not be null.pageable- must not be null.
-
getQuery
protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, org.springframework.data.domain.Sort sort) - Parameters:
spec- can be null.sort- must not be null.
-
getQuery
protected <S extends T> jakarta.persistence.TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Sort sort) - Parameters:
spec- can be null.domainClass- must not be null.sort- must not be null.
-
getCountQuery
@Deprecated protected jakarta.persistence.TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) Deprecated.overridegetCountQuery(Specification, Class)insteadCreates a new count query for the givenSpecification.- Parameters:
spec- can be null.
-
getCountQuery
protected <S extends T> jakarta.persistence.TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) Creates a new count query for the givenSpecification.- Parameters:
spec- can be null.domainClass- must not be null.
-
getQueryHints
ReturnsQueryHintswith the query hints based on the currentCrudMethodMetadataand potentialEntityGraphinformation. -
getQueryHintsForCount
ReturnsQueryHintswith the query hints on the currentCrudMethodMetadatafor count queries.
-
getCountQuery(Specification, Class)instead