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> CrudRepository<T,,ID> ListCrudRepository<T,,ID> ListPagingAndSortingRepository<T,,ID> PagingAndSortingRepository<T,,ID> QueryByExampleExecutor<T>,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 em) 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>
longlongcount(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>
booleanbooleanexists(Specification<T> spec) Checks whether the data store contains elements that match the givenSpecification.booleanexistsById(ID id) findAll()findAll(Specification<T> spec) Returns all entities matching the givenSpecification.findAll(Specification<T> spec, Pageable pageable) Returns aPageof entities matching the givenSpecification.findAll(Specification<T> spec, Sort sort) Returns all entities matching the givenSpecificationandSort.findAllById(Iterable<ID> ids) <S extends T,R>
RfindBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) <S extends T,R>
RfindBy(Specification<T> spec, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) Returns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.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, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.protected <S extends T>
jakarta.persistence.TypedQuery<S>getQuery(Specification<S> spec, Class<S> domainClass, Sort sort) protected jakarta.persistence.TypedQuery<T>getQuery(Specification<T> spec, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.protected jakarta.persistence.TypedQuery<T>getQuery(Specification<T> spec, 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 CrudMethodMetadatareadPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, Pageable pageable, Specification<S> spec) readPage(jakarta.persistence.TypedQuery<T> query, 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.em- 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
- Specified by:
deleteByIdin interfaceCrudRepository<T,ID>
-
delete
- Specified by:
deletein interfaceCrudRepository<T,ID>
-
deleteAllById
- Specified by:
deleteAllByIdin interfaceCrudRepository<T,ID>
-
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
- Specified by:
deleteAllin interfaceCrudRepository<T,ID>
-
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.- Specified by:
deleteAllInBatchin interfaceJpaRepository<T,ID> - Parameters:
entities- entities to be deleted. Must not be null.
-
deleteAll
- Specified by:
deleteAllin interfaceCrudRepository<T,ID>
-
deleteAllInBatch
Description copied from interface:JpaRepositoryDeletes all entities in a batch call.- Specified by:
deleteAllInBatchin interfaceJpaRepository<T,ID>
-
findById
- Specified by:
findByIdin interfaceCrudRepository<T,ID>
-
getQueryHints
ReturnsQueryHintswith the query hints based on the currentCrudMethodMetadataand potentialEntityGraphinformation. -
getQueryHintsForCount
ReturnsQueryHintswith the query hints on the currentCrudMethodMetadatafor count queries. -
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
- Specified by:
existsByIdin interfaceCrudRepository<T,ID>
-
findAll
- Specified by:
findAllin interfaceCrudRepository<T,ID> - Specified by:
findAllin interfaceListCrudRepository<T,ID>
-
findAllById
- Specified by:
findAllByIdin interfaceCrudRepository<T,ID> - Specified by:
findAllByIdin interfaceListCrudRepository<T,ID>
-
findAll
- Specified by:
findAllin interfaceListPagingAndSortingRepository<T,ID> - Specified by:
findAllin interfacePagingAndSortingRepository<T,ID>
-
findAll
- Specified by:
findAllin interfacePagingAndSortingRepository<T,ID>
-
findOne
Description copied from interface:JpaSpecificationExecutorReturns a single entity matching the givenSpecificationorOptional.empty()if none found.- Specified by:
findOnein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- can be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutorReturns all entities matching the givenSpecification.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- can be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutorReturns aPageof entities matching the givenSpecification.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- can 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- can be null.sort- must not be null.- Returns:
- never null.
-
findOne
- Specified by:
findOnein interfaceQueryByExampleExecutor<T>
-
count
- Specified by:
countin interfaceQueryByExampleExecutor<T>
-
exists
- Specified by:
existsin interfaceQueryByExampleExecutor<T>
-
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. Must 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.- Specified by:
deletein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theSpecificationto use for the existence check. Must not be null.- Returns:
- the number of entities deleted
-
findAll
- Specified by:
findAllin interfaceJpaRepository<T,ID> - Specified by:
findAllin interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAllin interfaceJpaRepository<T,ID> - Specified by:
findAllin interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAllin interfaceQueryByExampleExecutor<T>
-
findBy
public <S extends T,R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) - Specified by:
findByin interfaceQueryByExampleExecutor<T>
-
findBy
public <S extends T,R> R findBy(Specification<T> spec, Function<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.
-
count
public long count()- Specified by:
countin interfaceCrudRepository<T,ID>
-
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. Can be null.- Returns:
- the number of instances.
-
save
- Specified by:
savein interfaceCrudRepository<T,ID>
-
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
- Specified by:
saveAllin interfaceCrudRepository<T,ID> - Specified by:
saveAllin interfaceListCrudRepository<T,ID>
-
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
Description copied from interface:JpaRepositoryFlushes all pending changes to the database.- Specified by:
flushin interfaceJpaRepository<T,ID>
-
readPage
@Deprecated protected Page<T> readPage(jakarta.persistence.TypedQuery<T> query, 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> Page<S> readPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, 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, 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, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.- Parameters:
spec- can be null.domainClass- must not be null.pageable- must not be null.
-
getQuery
- 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, 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.
-
getCountQuery(Specification, Class)instead