Interface CrudRepository<E,ID>
- Type Parameters:
E- The entity typeID- 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
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 asNOT NULLor a foreign key (JDBC and R2DBC).OptimisticLockException: an update or delete of an entity with aVersionproperty 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 returningOptionalor a@Nullabletype return empty ornullinstead.DataAccessException: any other JDBC error, with the originalSQLExceptionas 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 TypeMethodDescriptionlongcount()Returns the number of entities available.voidDeletes a given entity.voidDeletes all entities managed by the repository.voidDeletes the given entities.voiddeleteById(ID id) Deletes the entity with the given id.booleanexistsById(ID id) Returns whether an entity with the given id exists.findAll()Returns all instances of the type.Retrieves an entity by its id.<S extends E>
Sinsert(S entity) This method issues an explicit insert for the given entity.This method issues an explicit insert for the given entities.<S extends E>
Ssave(S entity) Saves the given valid entity, returning a possibly new entity representing the saved state.Saves all given entities, possibly returning new instances representing the saved state.<S extends E>
Supdate(S entity) This method issues an explicit update for the given entity.This method issues an explicit update for the given entities.
-
Method Details
-
save
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)orupdate(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 nullEntityExistsException- if an insert violates a primary key or unique constraintOptimisticLockException- if an update of a versioned entity matches no rowDataAccessException- if the datastore reports another error
-
insert
This method issues an explicit insert for the given entity. The method differs fromsave(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 nullEntityExistsException- if the insert violates a primary key or unique constraintDataIntegrityViolationException- if the insert violates another integrity constraintDataAccessException- if the datastore reports another error- Since:
- 5.0.0
-
update
This method issues an explicit update for the given entity. The method differs fromsave(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
Versionproperty, the update only matches the row with the same version and fails withOptimisticLockExceptionotherwise. 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 nullOptimisticLockException- if the entity is versioned and no row with the same ID and version existsDataIntegrityViolationException- if the update violates an integrity constraintDataAccessException- if the datastore reports another error
-
updateAll
This method issues an explicit update for the given entities. The method differs fromsaveAll(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 nullOptimisticLockException- if the entities are versioned and fewer rows than entities were updatedDataAccessException- if the datastore reports another error- See Also:
-
insertAll
This method issues an explicit insert for the given entities. The method differs fromsaveAll(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 nullEntityExistsException- if an insert violates a primary key or unique constraintDataIntegrityViolationException- if an insert violates another integrity constraintDataAccessException- if the datastore reports another error- Since:
- 5.0.0
-
saveAll
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 nullDataAccessException- for the same reasons assave(Object)
-
findById
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
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
-
count
long count()Returns the number of entities available.- Returns:
- the number of entities
-
deleteById
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
Deletes a given entity.If the entity has a
Versionproperty, only the row with the same version is deleted andOptimisticLockExceptionis 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 nullOptimisticLockException- if the entity is versioned and no row with the same ID and version exists
-
deleteAll
Deletes the given entities.- Parameters:
entities- The entities to delete- Throws:
IllegalArgumentException- if the entities are nullOptimisticLockException- if the entities are versioned and fewer rows than entities were deleted- See Also:
-
deleteAll
void deleteAll()Deletes all entities managed by the repository.
-