Interface CrudRepository<E,ID>

Type Parameters:
E - The entity type
ID - The ID type
All Superinterfaces:
GenericRepository<E,ID>
All Known Subinterfaces:
AccountRecordRepository, AccountRepository, ArraysEntityRepository, AuthenticationRepository, AuthorRepository, AuthorRepository, AutoPopulatedUpsertRepository, BasicTypesRepository, BasicTypesRepository, BookEntityRepository, BookRepository, CarRepository, CatalogRepository, CategoryRepository, ChapterRepository, ChildRepository, CitizenRepository, CityRepository, ClientCategoryRepository, ClientRepository, ClinicRepository, ClinicServiceOfferingRepository, CompanyRepository, CompositeClinicOfferingRepository, CompositeClinicRepository, CountryRepository, CountryRepository, CustomerProfileRepository, CustomerProfileUuidRepository, DeliveryDriverJsonRepository, DeliveryDriverWktRepository, DeviceRepository, DocumentRepository, DomainEventsRepository, DomainEventsRepository, EmbeddedConflictEntityRepository, EmbeddedOwnerRepository, EmployeeFieldAccessRepository, EmployeeMixedAccessEmbeddedIdRepository, EmployeeMixedAccessRepository, EmployeePropertyAccessRepository, EntityWithIdClass2Repository, EntityWithIdClassRepository, FaceRepository, FoodRepository, GenreRepository, GeometryEntityJsonRepository, GeometryEntityWktRepository, HotelJsonRepository, HotelWktRepository, HouseEntityRepository, IntervalRepository, JpaRepository<E,ID>, JsonEntityRepository, MealRepository, MultiArrayEntityRepository, NoseRepository, OrderRepository, PageableRepository<E,ID>, PatientRepository, PersonRepository, PersonRepository, ProductRepository, ProductReviewRepository, ProjectRepository, PublisherRepository, PurchaseOrderRepository, RegionRepository, RestaurantRepository, RestaurantRepository, RoleRepository, SaleItemRepository, SaleRepository, SaleRepository, SchoolRepository, SettlementRepository, SettlementTypeRepository, ShipmentRepository, StreamingPersonRepository, StudentRepository, StudentRepository, TimezoneBasicTypesRepository, UserRepository, UuidRepository, WarehouseInventoryRepository, ZoneRepository
All Known Implementing Classes:
BookRepository, BookRepository

public interface CrudRepository<E,ID> extends GenericRepository<E,ID>
A repository interface for performing CRUD (Create, Read, Update, Delete) operations on entities of type E identified by values of type ID.

Declare an interface that extends this one and annotate it with a repository annotation such as @JdbcRepository, @R2dbcRepository, @MongoRepository or @Repository (JPA). Micronaut Data implements the interface at compile time; each method executes its operation against the datastore and participates in the current transaction, if one is active. The methods of this interface block the calling thread until the operation completes. See AsyncCrudRepository, ReactorCrudRepository and ReactiveStreamsCrudRepository for non-blocking variants.

Entities and identifiers can be validated before they reach the datastore by annotating the type arguments with Jakarta Validation constraints, for example CrudRepository<@Valid Book, @NotNull Long>. Validation requires Micronaut Validation on the classpath and fails with jakarta.validation.ConstraintViolationException.

Exceptions

All exceptions are unchecked. Micronaut Data reports datastore failures with subclasses of DataAccessException:

  • EntityExistsException: an insert violated a primary key or unique constraint (JDBC and R2DBC).
  • DataIntegrityViolationException: a write violated another integrity constraint, such as NOT NULL or a foreign key (JDBC and R2DBC).
  • OptimisticLockException: an update or delete of an entity with a Version property matched no row, because the row was changed or deleted concurrently.
  • EmptyResultException: a query method declared to return a non-null single result found nothing. Methods returning Optional or a @Nullable type return empty or null instead.
  • DataAccessException: any other JDBC error, with the original SQLException as the cause. R2DBC and MongoDB driver exceptions that are not mapped to one of the types above are propagated unchanged.

null arguments are rejected with IllegalArgumentException before any statement is executed, whether the argument is an ID, an entity or the collection of entities, or with ConstraintViolationException if the type argument carries a @NotNull constraint and Micronaut Validation is present. JPA based implementations (Hibernate) propagate the exceptions of the JPA provider, for example jakarta.persistence.OptimisticLockException, and may report constraint violations only when the persistence context is flushed, which is often when the transaction commits rather than when the repository method returns.

Since:
1.0
Author:
graemerocher
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of entities available.
    void
    delete(E entity)
    Deletes a given entity.
    void
    Deletes all entities managed by the repository.
    void
    deleteAll(Iterable<? extends E> entities)
    Deletes the given entities.
    void
    Deletes the entity with the given id.
    boolean
    Returns whether an entity with the given id exists.
    Returns all instances of the type.
    Retrieves an entity by its id.
    <S extends E>
    S
    insert(S entity)
    This method issues an explicit insert for the given entity.
    <S extends E>
    List<S>
    insertAll(Iterable<S> entities)
    This method issues an explicit insert for the given entities.
    <S extends E>
    S
    save(S entity)
    Saves the given valid entity, returning a possibly new entity representing the saved state.
    <S extends E>
    List<S>
    saveAll(Iterable<S> entities)
    Saves all given entities, possibly returning new instances representing the saved state.
    <S extends E>
    S
    update(S entity)
    This method issues an explicit update for the given entity.
    <S extends E>
    List<S>
    updateAll(Iterable<S> entities)
    This method issues an explicit update for the given entities.
  • Method Details

    • save

      <S extends E> S save(S entity)
      Saves the given valid entity, returning a possibly new entity representing the saved state.

      If the entity has no identity value, an insert is performed. If the entity has a generated or always auto-populated identity value already present, an update is attempted. Entities with non-generated assigned identities are inserted by default. To require a specific operation, use insert(Object) or update(Object). This is the default repository save behavior and can be overridden by Micronaut Data configuration.

      Type Parameters:
      S - The generic type
      Parameters:
      entity - The entity to save. Must not be null.
      Returns:
      The saved entity will never be null.
      Throws:
      IllegalArgumentException - if the entity is null
      EntityExistsException - if an insert violates a primary key or unique constraint
      OptimisticLockException - if an update of a versioned entity matches no row
      DataAccessException - if the datastore reports another error
    • insert

      <S extends E> S insert(S entity)
      This method issues an explicit insert for the given entity. The method differs from save(Object) in that an insert will be generated regardless of the entity identity state. If the entity already exists then an exception may be thrown.
      Type Parameters:
      S - The generic type
      Parameters:
      entity - The entity to insert. Must not be null.
      Returns:
      The inserted entity will never be null.
      Throws:
      IllegalArgumentException - if the entity is null
      EntityExistsException - if the insert violates a primary key or unique constraint
      DataIntegrityViolationException - if the insert violates another integrity constraint
      DataAccessException - if the datastore reports another error
      Since:
      5.0.0
    • update

      <S extends E> S update(S entity)
      This method issues an explicit update for the given entity. The method differs from save(Object) in that an update will be generated regardless of the entity identity state. If the entity has no assigned ID then an exception will be thrown.

      If the entity has a Version property, the update only matches the row with the same version and fails with OptimisticLockException otherwise. Without a version property, SQL and MongoDB repositories treat an update that matches no row as a no-op.

      Type Parameters:
      S - The generic type
      Parameters:
      entity - The entity to update. Must not be null.
      Returns:
      The updated entity will never be null.
      Throws:
      IllegalArgumentException - if the entity is null
      OptimisticLockException - if the entity is versioned and no row with the same ID and version exists
      DataIntegrityViolationException - if the update violates an integrity constraint
      DataAccessException - if the datastore reports another error
    • updateAll

      <S extends E> List<S> updateAll(Iterable<S> entities)
      This method issues an explicit update for the given entities. The method differs from saveAll(Iterable) in that an update will be generated for every entity regardless of identity state. If an entity has no assigned ID then an exception will be thrown.
      Type Parameters:
      S - The generic type
      Parameters:
      entities - The entities to update. Must not be null.
      Returns:
      The updated entities will never be null.
      Throws:
      IllegalArgumentException - if the entities are null
      OptimisticLockException - if the entities are versioned and fewer rows than entities were updated
      DataAccessException - if the datastore reports another error
      See Also:
    • insertAll

      <S extends E> List<S> insertAll(Iterable<S> entities)
      This method issues an explicit insert for the given entities. The method differs from saveAll(Iterable) in that an insert will be generated for every entity regardless of identity state. If an entity already exists then an exception may be thrown.
      Type Parameters:
      S - The generic type
      Parameters:
      entities - The entities to insert. Must not be null.
      Returns:
      The inserted entities will never be null.
      Throws:
      IllegalArgumentException - if the entities are null
      EntityExistsException - if an insert violates a primary key or unique constraint
      DataIntegrityViolationException - if an insert violates another integrity constraint
      DataAccessException - if the datastore reports another error
      Since:
      5.0.0
    • saveAll

      <S extends E> List<S> saveAll(Iterable<S> entities)
      Saves all given entities, possibly returning new instances representing the saved state.

      Each entity is saved independently using the same rules as save(Object). This is the default repository save behavior and can be overridden by Micronaut Data configuration.

      Type Parameters:
      S - The generic type
      Parameters:
      entities - The entities to save. Must not be null.
      Returns:
      The saved entities objects. will never be null.
      Throws:
      IllegalArgumentException - if the entities are null
      DataAccessException - for the same reasons as save(Object)
    • findById

      Optional<E> findById(ID id)
      Retrieves an entity by its id.
      Parameters:
      id - The ID of the entity to retrieve. Must not be null.
      Returns:
      the entity with the given id or Optional#empty() if none found
      Throws:
      IllegalArgumentException - if the ID is null
    • existsById

      boolean existsById(ID id)
      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:
      IllegalArgumentException - if the ID is null
    • findAll

      List<E> findAll()
      Returns all instances of the type.
      Returns:
      all entities
    • count

      long count()
      Returns the number of entities available.
      Returns:
      the number of entities
    • deleteById

      void deleteById(ID id)
      Deletes the entity with the given id. Deleting an ID that does not exist is a no-op.
      Parameters:
      id - must not be null.
      Throws:
      IllegalArgumentException - if the ID is null
    • delete

      void delete(E entity)
      Deletes a given entity.

      If the entity has a Version property, only the row with the same version is deleted and OptimisticLockException is thrown if there is none. Without a version property, SQL and MongoDB repositories treat deleting an entity that does not exist as a no-op.

      Parameters:
      entity - The entity to delete
      Throws:
      IllegalArgumentException - if the entity is null
      OptimisticLockException - if the entity is versioned and no row with the same ID and version exists
    • deleteAll

      void deleteAll(Iterable<? extends E> entities)
      Deletes the given entities.
      Parameters:
      entities - The entities to delete
      Throws:
      IllegalArgumentException - if the entities are null
      OptimisticLockException - if the entities are versioned and fewer rows than entities were deleted
      See Also:
    • deleteAll

      void deleteAll()
      Deletes all entities managed by the repository.