Class 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>, JpaRepositoryConfigurationAware, JpaRepositoryImplementation<T, ID>, CrudRepository<T, ID>, ListCrudRepository<T, ID>, ListPagingAndSortingRepository<T, ID>, PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>, Repository<T, ID>
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, Seol-JY, Joshua Chen, Giheon Do
-
Nested Class Summary
Nested classes/interfaces inherited from interface JpaSpecificationExecutor
JpaSpecificationExecutor.SpecificationFluentQuery<T> -
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>
longlongcount(Specification<T> spec) Returns the number of instances that the givenSpecificationwill return.longdelete(DeleteSpecification<T> spec) Deletes by theUpdateSpecificationand 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(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) Returns aPageof entities matching the givenSpecification.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) findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) findBy(Specification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<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.protected <S> jakarta.persistence.QuerygetDelete(DeleteSpecification<S> spec, Class<S> domainClass) Creates aQueryfor the givenDeleteSpecification.Deprecated.protected <S extends T>
jakarta.persistence.TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.protected <S extends T>
jakarta.persistence.TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, 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 @Nullable CrudMethodMetadataprotected <S> jakarta.persistence.QuerygetUpdate(UpdateSpecification<S> spec, Class<S> domainClass) Creates aQueryfor the givenUpdateSpecification.readPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, Pageable pageable, @Nullable 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.voidsetProjectionFactory(ProjectionFactory projectionFactory) Configures theProjectionFactoryto be used with the repository.voidConfigures a customCrudMethodMetadatato be used to detectLockModeTypes and query hints to be applied to queries.longupdate(UpdateSpecification<T> spec) Updates entities by theUpdateSpecificationand returns the number of rows updated.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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 interfaceJpaRepositoryConfigurationAware- Parameters:
metadata-
-
setEscapeCharacter
Description copied from interface:JpaRepositoryConfigurationAwareConfigures theEscapeCharacterto be used with the repository.- Specified by:
setEscapeCharacterin interfaceJpaRepositoryConfigurationAware- Parameters:
escapeCharacter- must not be null.
-
setProjectionFactory
Description copied from interface:JpaRepositoryConfigurationAwareConfigures theProjectionFactoryto be used with the repository.- Specified by:
setProjectionFactoryin interfaceJpaRepositoryConfigurationAware- Parameters:
projectionFactory- 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.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
- 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
-
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:
-
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:
-
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:
-
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
-
findAll
-
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.
- See Also:
-
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.
- See Also:
-
findAll
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.
- See Also:
-
findAll
public Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) Description copied from interface:JpaSpecificationExecutorReturns aPageof entities matching the givenSpecification.Supports counting the total number of entities matching the
Specification.- Specified by:
findAllin interfaceJpaSpecificationExecutor<T>- Parameters:
spec- can be null, if noSpecificationis given all entities matching<T>will be selected.countSpec- can be null,if noSpecificationis given all entities matching<T>will be counted.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.
- See Also:
-
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.- See Also:
-
update
Description copied from interface:JpaSpecificationExecutorUpdates entities by theUpdateSpecificationand returns the number of rows updated.This method uses
Criteria API bulk updatethat maps directly to database update operations. The persistence context is not synchronized with the result of the bulk update.- Specified by:
updatein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theUpdateSpecificationto use for the update query must not be null.- Returns:
- the number of entities deleted.
-
delete
Description copied from interface:JpaSpecificationExecutorDeletes by theUpdateSpecificationand 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.- Specified by:
deletein interfaceJpaSpecificationExecutor<T>- Parameters:
spec- theUpdateSpecificationto use for the delete query must not be null.- Returns:
- the number of entities deleted.
- See Also:
-
findBy
public <S extends T, R extends @Nullable Object> R findBy(Specification<T> spec, Function<? super JpaSpecificationExecutor.SpecificationFluentQuery<S>, R> queryFunction) Description copied from interface:JpaSpecificationExecutorReturns entities matching the givenSpecificationapplying thequeryFunctionthat defines the query and its result type.The query object used with
queryFunctionis only valid inside thefindBy(…)method call. This requires the query function to return a query result and not theFluentQueryobject itself to ensure the query is executed inside thefindBy(…)method.- 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 specification.
-
findOne
-
count
- Specified by:
countin interfaceQueryByExampleExecutor<T>
-
exists
- Specified by:
existsin interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAllin interfaceJpaRepository<T,ID> - Specified by:
findAllin interfaceQueryByExampleExecutor<T>
-
findAll
-
findAll
-
findBy
public <S extends T, R extends @Nullable Object> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) - Specified by:
findByin interfaceQueryByExampleExecutor<T>
-
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, must not be null.- Returns:
- the number of instances.
- See Also:
-
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, Specification<T> spec) Deprecated.- Parameters:
query- must not be null.pageable- must not be null.spec- can be null.
-
readPage
@Contract("_, _, _, null -> fail") 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.pageable- can be null.spec- must not be null.
-
getQuery
protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.- Parameters:
spec- must not be null.pageable- must not be null.
-
getQuery
protected <S extends T> jakarta.persistence.TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, Pageable pageable) Creates a newTypedQueryfrom the givenSpecification.- Parameters:
spec- must not be null.domainClass- must not be null.pageable- must not be null.
-
getQuery
- Parameters:
spec- must not 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- must not be null.domainClass- must not be null.sort- must not be null.
-
getUpdate
protected <S> jakarta.persistence.Query getUpdate(UpdateSpecification<S> spec, Class<S> domainClass) Creates aQueryfor the givenUpdateSpecification.- Parameters:
spec- must not be null.domainClass- must not be null.
-
getDelete
protected <S> jakarta.persistence.Query getDelete(DeleteSpecification<S> spec, Class<S> domainClass) Creates aQueryfor the givenDeleteSpecification.- Parameters:
spec- must not be null.domainClass- must not be null.
-
getCountQuery
Deprecated.overridegetCountQuery(Specification, Class)insteadCreates a new count query for the givenSpecification.- Parameters:
spec- must not be null.
-
getCountQuery
protected <S extends T> jakarta.persistence.TypedQuery<Long> getCountQuery(Specification<S> spec, Class<S> domainClass) Creates a new count query for the givenSpecification.- Parameters:
spec- must not 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