Interface BasicRepository<T,K>
- Type Parameters:
T
- the entity bean typeK
- the key type.
- All Superinterfaces:
DataRepository<T,
K>
- All Known Subinterfaces:
CrudRepository<T,
,K> PageableRepository<T,
K>
A repository interface for performing basic operations on entities.
This repository provides methods to interact with persistent entities of type <T>
,
where <T>
represents the entity bean type, and <K>
represents the key type.
-
Method Summary
Modifier and TypeMethodDescriptionlong
count()
Retrieves the total number of persistent entities of the specified type in the database.void
Deletes a given entity.void
Deletes all persistent entities managed by the repository.void
Deletes the given entities.void
deleteById
(K id) Deletes the entity with the given id.void
deleteByIdIn
(Iterable<K> ids) Deletes all instances of the typeT
with the given IDs.boolean
existsById
(K id) Returns whether an entity with the given id exists.findAll()
Retrieves all persistent entities of the specified type from the database.Retrieves an entity by its id.findByIdIn
(Iterable<K> ids) Returns all instances of the typeT
with the given IDs.<S extends T>
Ssave
(S entity) Saves a given entity to the database.Saves all given entities to the database.
-
Method Details
-
save
Saves a given entity to the database. If the entity has an ID or key that exists in the database, the method will update the existing record. Otherwise, it will insert a new record.If the entity has a non-null ID, the method will attempt to update the existing record in the database. If the entity does not exist in the database or has a null ID, then this method will insert a new record into the database.
The entity instance that is returned as a result value of this method must be updated with all automatically generated values and incremented values that changed due to the save. After invoking this method, do not continue to use the entity value that is supplied as a parameter. This method makes no guarantees about the state of the entity value that is supplied as a parameter.
If the entity uses optimistic locking and the version differs from the version in the database, an
OptimisticLockingFailureException
will be thrown.- Type Parameters:
S
- Type of the entity to save.- Parameters:
entity
- The entity to be saved. Must not be null.- Returns:
- The saved entity; never null.
- Throws:
OptimisticLockingFailureException
- If the entity uses optimistic locking and the version in the database differs from the version in the entity.NullPointerException
- If the provided entity is null.
-
saveAll
Saves all given entities to the database. If an entity has a non-null ID that exists in the database, the method will update the existing record. Otherwise, it will insert a new record.If an entity has a non-null ID, this method will attempt to update the existing record in the database. If an entity does not exist in the database or has a null ID, then this method inserts a new record into the database.
The entity instances that are returned as a result of this method must be updated with all automatically generated values and incremented values that changed due to the save. After invoking this method, do not continue to use the entity values that are supplied in the parameter. This method makes no guarantees about the state of the entity values that are supplied in the parameter.
- Type Parameters:
S
- Type of entity to save.- Parameters:
entities
- An iterable of entities.- Returns:
- The saved entities; will never be null.
- Throws:
OptimisticLockingFailureException
- If an entity has a version for optimistic locking that differs from the version in the database.NullPointerException
- If either the iterable is null or any element is null.
-
findById
Retrieves an entity by its id.- Parameters:
id
- must not be null.- Returns:
- the entity with the given id or Optional#empty() if none found.
- Throws:
NullPointerException
- when the id is null
-
existsById
Returns whether an entity with the given id exists.- Parameters:
id
- must not be null.- Returns:
- true if an entity with the given id exists, false otherwise.
- Throws:
NullPointerException
- when the ID is null
-
findAll
Retrieves all persistent entities of the specified type from the database.- Returns:
- a stream of all entities; will never be null.
- Throws:
UnsupportedOperationException
- for Key-Value and Wide-Column databases that are not capable of thefindAll
operation.
-
findByIdIn
Returns all instances of the typeT
with the given IDs.If some or all ids are not found, no entities are returned for these IDs.
Note that the order of elements in the result is not guaranteed.
- Parameters:
ids
- must not be null nor contain any null values.- Returns:
- guaranteed to be not null. The size can be equal or less than the number of given ids.
- Throws:
NullPointerException
- in case the givenids
or one of its items is null.
-
count
long count()Retrieves the total number of persistent entities of the specified type in the database.- Returns:
- the total number of entities.
- Throws:
UnsupportedOperationException
- for Key-Value and Wide-Column databases that are not capable of thecount
operation.
-
deleteById
Deletes the entity with the given id.If the entity is not found in the persistence store it is silently ignored.
- Parameters:
id
- must not be null.- Throws:
NullPointerException
- when the id is null
-
delete
Deletes a given entity. Deletion is performed by matching the Id, and if the entity is versioned (for example, withjakarta.persistence.Version
), then also the version. Other properties of the entity do not need to match.- Parameters:
entity
- must not be null.- Throws:
OptimisticLockingFailureException
- if the entity is not found in the database for deletion or has a version for optimistic locking that is inconsistent with the version in the database.NullPointerException
- when the entity is null
-
deleteByIdIn
Deletes all instances of the typeT
with the given IDs.Entities that aren't found in the persistence store are silently ignored.
- Parameters:
ids
- must not be null. Must not contain null elements.- Throws:
NullPointerException
- when either the iterable is null or contains null elements
-
deleteAll
Deletes the given entities. Deletion of each entity is performed by matching the ID, and if the entity is versioned (for example, withjakarta.persistence.Version
), then also the version. Other properties of the entity do not need to match.- Parameters:
entities
- Must not be null. Must not contain null elements.- Throws:
OptimisticLockingFailureException
- If an entity is not found in the database for deletion or has a version for optimistic locking that is inconsistent with the version in the database.NullPointerException
- If either the iterable is null or contains null elements.
-
deleteAll
void deleteAll()Deletes all persistent entities managed by the repository.- Throws:
UnsupportedOperationException
- for Key-Value and Wide-Column databases that are not capable of thedeleteAll
operation.
-