A built-in repository supertype for performing Create, Read, Update, and Delete (CRUD) operations.
This repository extends the BasicRepository interface, adding insert and update
operations in addition to basic retrieval and deletion. This interface
combines the Data Access Object (DAO) aspect with the repository pattern, offering a versatile and complete solution
for managing persistent entities within Java applications.
The type parameters of CrudRepository<T,K> capture the primary entity type (T) for the repository
and the type of the unique identifier attribute (K) of the primary entity type.
The primary entity type is used for repository methods, such as countBy... and deleteBy..., which
do not explicitly specify an entity type.
Example entity:
@Entity
public class Cars {
@Id
public long vin;
public String make;
public String model;
public int year;
...
}
Example repository:
@Repository
public interface Cars extends CrudRepository<Car, Long> {
List<Car> findByMakeAndModel(String make, String model, Sort<?>... sorts);
...
}
Example usage:
@Inject
Cars cars;
...
Car car1 = ...
car1 = cars.insert(car1);
List<Car> found = findByMakeAndModel(car1.make,
car1.model,
Sort.desc("year"),
Sort.asc("vin"));
The module Javadoc provides an overview of Jakarta Data.
BasicRepository, DataRepository
| Modifier and Type | Method and Description |
|---|---|
| public < Type of the entity to insert. S extends T> S | Returns: the inserted entity, which may or may not be a different instance depending on whether the insert caused values to be generated or automatically incremented.the entity to insert. Must not be entity)null.Inserts an entity into the database. |
| public < Type of the entities to insert. S extends T> List | |
| public < Type of the entity to update. S extends T> S | Returns: an updated entity instance including all automatically generated values, updated versions, and incremented values which changed as a result of the update.the entity to update. Must not be entity)null.Modifies an entity that already exists in the database. |
| public < Type of the entities to update. S extends T> List | Returns: updated entity instances, in the same order as the supplied entities, and including all automatically generated values, updated versions, and incremented values which changed as a result of the update.entities to update. entities)Modifies entities that already exist in the database. |
| insert | back to summary |
|---|---|
| public <S extends T> S insert(S entity) Inserts an entity into the database. If an entity of this type with the same
unique identifier already exists in the database and the database supports ACID transactions,
then this method raises The entity instance returned as a result of this method must include all values that were written to the database, including all automatically generated values and incremented values that changed due to the insert. After invoking this method, do not continue to use the instance that is supplied as a parameter. This method makes no guarantees about the state of the instance that is supplied as a parameter.
| |
| insertAll | back to summary |
|---|---|
| public <S extends T> List Inserts multiple entities into the database. If any entity of this type with the same
unique identifier as any of the given entities already exists in the database and the database
supports ACID transactions, then this method raises The entities within the returned
| |
| update | back to summary |
|---|---|
| public <S extends T> S update(S entity) Modifies an entity that already exists in the database. For an update to be made, a matching entity with the same unique identifier
must be present in the database. In databases that use an append model to write data or
follow the BASE model, this method behaves the same as the If the entity is versioned (for example, with
| |
| updateAll | back to summary |
|---|---|
| public <S extends T> List Modifies entities that already exist in the database. For an update to be made to an entity, a matching entity with the same unique identifier
must be present in the database. In databases that use an append model to write data or
follow the BASE model, this method behaves the same as the If the entity is versioned (for example, with
| |