Module jakarta.data

Interface CrudRepository<T,K>

Type Parameters:
T - the type of the primary entity class of the repository.
K - the type of the unique identifier field of property of the primary entity.
All Superinterfaces:
BasicRepository<T,K>, DataRepository<T,K>

public interface CrudRepository<T,K> extends BasicRepository<T,K>

A built-in repository supertype for performing Create, Read, Update, and Delete (CRUD) operations.

This repository extends the BasicRepository interface, adding insert(S) and update(S) 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@gt; {

     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.

See Also:
  • Method Details

    • insert

      @Insert <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 EntityExistsException. In databases that follow the BASE model or use an append model to write data, this exception is not thrown.

      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.

      Type Parameters:
      S - Type of the entity to insert.
      Parameters:
      entity - the entity to insert. Must not be null.
      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.
      Throws:
      EntityExistsException - if the entity is already present in the database (in ACID-supported databases).
      NullPointerException - if the entity is null.
    • insertAll

      @Insert <S extends T> List<S> insertAll(List<S> entities)

      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 EntityExistsException. In databases that follow the BASE model or use an append model to write data, this exception is not thrown.

      The entities within the returned Iterable 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 entity instances that are supplied in the parameter. This method makes no guarantees about the state of the entity instances that are supplied in the parameter. The position of entities within the Iterable return value must correspond to the position of entities in the parameter based on the unique identifier of the entity.

      Type Parameters:
      S - Type of the entities to insert.
      Parameters:
      entities - entities to insert.
      Returns:
      an iterable containing the inserted entities, which may or may not be different instances depending on whether the insert caused values to be generated or automatically incremented.
      Throws:
      EntityExistsException - if any of the entities are already present in the database (in ACID-supported databases).
      NullPointerException - if the iterable is null or any element is null.
    • update

      @Update <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 insert(S) method.

      If the entity is versioned (for example, with jakarta.persistence.Version or by another convention from the entity model such as having an attribute named version), then the version must also match. The version is automatically incremented when making the update.

      Type Parameters:
      S - Type of the entity to update.
      Parameters:
      entity - the entity to update. Must not be null.
      Returns:
      an updated entity instance including all automatically generated values, updated versions, and incremented values which changed as a result of the update.
      Throws:
      OptimisticLockingFailureException - the entity is not found in the database or has a version that differs from the version in the database.
      NullPointerException - if the entity is null.
    • updateAll

      @Update <S extends T> List<S> updateAll(List<S> entities)

      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 insertAll(java.util.List<S>) method.

      If the entity is versioned (for example, with jakarta.persistence.Version or by another convention from the entity model such as having an attribute named version), then the version must also match. The version is automatically incremented when making the update.

      Type Parameters:
      S - Type of the entities to update.
      Parameters:
      entities - entities to update.
      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.
      Throws:
      OptimisticLockingFailureException - If any of the supplied entities is not found in the database or has a version that differs from the version in the database.
      NullPointerException - if either the supplied Iterable is null or any element is null.