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 entityManager) Creates a newSimpleJpaRepository
to manage objects of the given domain type.SimpleJpaRepository
(JpaEntityInformation<T, ?> entityInformation, jakarta.persistence.EntityManager entityManager) Creates a newSimpleJpaRepository
to manage objects of the givenJpaEntityInformation
. -
Method Summary
Modifier and TypeMethodDescriptionlong
count()
<S extends T>
longlong
count
(Specification<T> spec) Returns the number of instances that the givenSpecification
will return.long
delete
(Specification<T> spec) Deletes by theSpecification
and returns the number of rows deleted.void
void
void
void
deleteAllById
(Iterable<? extends ID> ids) void
deleteAllByIdInBatch
(Iterable<ID> ids) Deletes the entities identified by the given ids using a single query.void
Deletes all entities in a batch call.void
deleteAllInBatch
(Iterable<T> entities) Deletes the given entities in a batch which means it will create a single query.void
deleteById
(ID id) <S extends T>
booleanboolean
exists
(Specification<T> spec) Checks whether the data store contains elements that match the givenSpecification
.boolean
existsById
(ID id) findAll()
findAll
(Specification<T> spec) Returns all entities matching the givenSpecification
.findAll
(Specification<T> spec, Pageable pageable) Returns aPage
of entities matching the givenSpecification
.findAll
(Specification<T> spec, Sort sort) Returns all entities matching the givenSpecification
andSort
.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 givenSpecification
applying thequeryFunction
that defines the query and its result type.findOne
(Specification<T> spec) Returns a single entity matching the givenSpecification
orOptional.empty()
if none found.void
flush()
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 newTypedQuery
from 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 newTypedQuery
from the givenSpecification
.protected jakarta.persistence.TypedQuery<T>
getQuery
(Specification<T> spec, Sort sort) protected QueryHints
ReturnsQueryHints
with the query hints based on the currentCrudMethodMetadata
and potentialEntityGraph
information.protected QueryHints
ReturnsQueryHints
with the query hints on the currentCrudMethodMetadata
for count queries.getReferenceById
(ID id) Returns a reference to the entity with the given identifier.protected CrudMethodMetadata
readPage
(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.void
setEscapeCharacter
(EscapeCharacter escapeCharacter) Configures theEscapeCharacter
to be used with the repository.void
setRepositoryMethodMetadata
(CrudMethodMetadata crudMethodMetadata) Configures a customCrudMethodMetadata
to be used to detectLockModeType
s 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
Methods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteInBatch
-
Constructor Details
-
SimpleJpaRepository
public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, jakarta.persistence.EntityManager entityManager) Creates a newSimpleJpaRepository
to manage objects of the givenJpaEntityInformation
.- Parameters:
entityInformation
- must not be null.entityManager
- must not be null.
-
SimpleJpaRepository
Creates a newSimpleJpaRepository
to manage objects of the given domain type.- Parameters:
domainClass
- must not be null.entityManager
- must not be null.
-
-
Method Details
-
setRepositoryMethodMetadata
Configures a customCrudMethodMetadata
to be used to detectLockModeType
s and query hints to be applied to queries.- Specified by:
setRepositoryMethodMetadata
in interfaceJpaRepositoryImplementation<T,
ID> - Parameters:
crudMethodMetadata
-
-
setEscapeCharacter
Description copied from interface:JpaRepositoryImplementation
Configures theEscapeCharacter
to be used with the repository.- Specified by:
setEscapeCharacter
in interfaceJpaRepositoryImplementation<T,
ID> - Parameters:
escapeCharacter
- Must not be null.
-
getRepositoryMethodMetadata
-
getDomainClass
-
deleteById
- Specified by:
deleteById
in interfaceCrudRepository<T,
ID>
-
delete
- Specified by:
delete
in interfaceCrudRepository<T,
ID>
-
deleteAllById
- Specified by:
deleteAllById
in interfaceCrudRepository<T,
ID>
-
deleteAllByIdInBatch
Description copied from interface:JpaRepository
Deletes 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 theEntityManager
before calling this method.- Specified by:
deleteAllByIdInBatch
in interfaceJpaRepository<T,
ID> - Parameters:
ids
- the ids of the entities to be deleted. Must not be null.
-
deleteAll
- Specified by:
deleteAll
in interfaceCrudRepository<T,
ID>
-
deleteAllInBatch
Description copied from interface:JpaRepository
Deletes 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 theEntityManager
before calling this method.It will also NOT honor cascade semantics of JPA, nor will it emit JPA lifecycle events.
- Specified by:
deleteAllInBatch
in interfaceJpaRepository<T,
ID> - Parameters:
entities
- entities to be deleted. Must not be null.
-
deleteAll
- Specified by:
deleteAll
in interfaceCrudRepository<T,
ID>
-
deleteAllInBatch
Description copied from interface:JpaRepository
Deletes all entities in a batch call.- Specified by:
deleteAllInBatch
in interfaceJpaRepository<T,
ID>
-
findById
- Specified by:
findById
in interfaceCrudRepository<T,
ID>
-
getOne
Deprecated.Description copied from interface:JpaRepository
Returns 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 anEntityNotFoundException
on first access. Some of them will reject invalid identifiers immediately.- Specified by:
getOne
in 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:JpaRepository
Returns 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 anEntityNotFoundException
on first access. Some of them will reject invalid identifiers immediately.- Specified by:
getById
in 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:JpaRepository
Returns 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 anEntityNotFoundException
on first access. Some of them will reject invalid identifiers immediately.- Specified by:
getReferenceById
in 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:
existsById
in interfaceCrudRepository<T,
ID>
-
findAll
- Specified by:
findAll
in interfaceCrudRepository<T,
ID> - Specified by:
findAll
in interfaceListCrudRepository<T,
ID>
-
findAllById
- Specified by:
findAllById
in interfaceCrudRepository<T,
ID> - Specified by:
findAllById
in interfaceListCrudRepository<T,
ID>
-
findAll
- Specified by:
findAll
in interfaceListPagingAndSortingRepository<T,
ID> - Specified by:
findAll
in interfacePagingAndSortingRepository<T,
ID>
-
findAll
- Specified by:
findAll
in interfacePagingAndSortingRepository<T,
ID>
-
findOne
Description copied from interface:JpaSpecificationExecutor
Returns a single entity matching the givenSpecification
orOptional.empty()
if none found.- Specified by:
findOne
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- must not be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutor
Returns all entities matching the givenSpecification
.- Specified by:
findAll
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- must not be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutor
Returns aPage
of entities matching the givenSpecification
.- Specified by:
findAll
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- must not be null.pageable
- must not be null.- Returns:
- never null.
-
findAll
Description copied from interface:JpaSpecificationExecutor
Returns all entities matching the givenSpecification
andSort
.- Specified by:
findAll
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- must not be null.sort
- must not be null.- Returns:
- never null.
-
exists
Description copied from interface:JpaSpecificationExecutor
Checks whether the data store contains elements that match the givenSpecification
.- Specified by:
exists
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- theSpecification
to use for the existence check, ust not be null.- Returns:
true
if the data store contains elements that match the givenSpecification
otherwisefalse
.
-
delete
Description copied from interface:JpaSpecificationExecutor
Deletes by theSpecification
and returns the number of rows deleted.This method uses
Criteria API bulk delete
that maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.Please note that
CriteriaQuery
in,Specification.toPredicate(Root, CriteriaQuery, CriteriaBuilder)
will be null becauseCriteriaBuilder.createCriteriaDelete(Class)
does not implementCriteriaQuery
.- Specified by:
delete
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- theSpecification
to 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<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) Description copied from interface:JpaSpecificationExecutor
Returns entities matching the givenSpecification
applying thequeryFunction
that defines the query and its result type.- Specified by:
findBy
in 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:
findOne
in interfaceQueryByExampleExecutor<T>
-
count
- Specified by:
count
in interfaceQueryByExampleExecutor<T>
-
exists
- Specified by:
exists
in interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAll
in interfaceJpaRepository<T,
ID> - Specified by:
findAll
in interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAll
in interfaceJpaRepository<T,
ID> - Specified by:
findAll
in interfaceQueryByExampleExecutor<T>
-
findAll
- Specified by:
findAll
in interfaceQueryByExampleExecutor<T>
-
findBy
public <S extends T,R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) - Specified by:
findBy
in interfaceQueryByExampleExecutor<T>
-
count
public long count()- Specified by:
count
in interfaceCrudRepository<T,
ID>
-
count
Description copied from interface:JpaSpecificationExecutor
Returns the number of instances that the givenSpecification
will return.- Specified by:
count
in interfaceJpaSpecificationExecutor<T>
- Parameters:
spec
- theSpecification
to count instances for, must not be null.- Returns:
- the number of instances.
-
save
- Specified by:
save
in interfaceCrudRepository<T,
ID>
-
saveAndFlush
Description copied from interface:JpaRepository
Saves an entity and flushes changes instantly.- Specified by:
saveAndFlush
in interfaceJpaRepository<T,
ID> - Parameters:
entity
- entity to be saved. Must not be null.- Returns:
- the saved entity
-
saveAll
- Specified by:
saveAll
in interfaceCrudRepository<T,
ID> - Specified by:
saveAll
in interfaceListCrudRepository<T,
ID>
-
saveAllAndFlush
Description copied from interface:JpaRepository
Saves all entities and flushes changes instantly.- Specified by:
saveAllAndFlush
in interfaceJpaRepository<T,
ID> - Parameters:
entities
- entities to be saved. Must not be null.- Returns:
- the saved entities
-
flush
Description copied from interface:JpaRepository
Flushes all pending changes to the database.- Specified by:
flush
in 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 newTypedQuery
from 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 newTypedQuery
from 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.
-
getQueryHints
ReturnsQueryHints
with the query hints based on the currentCrudMethodMetadata
and potentialEntityGraph
information. -
getQueryHintsForCount
ReturnsQueryHints
with the query hints on the currentCrudMethodMetadata
for count queries.
-
getCountQuery(Specification, Class)
instead