- Type Parameters:
E- the type of entity managed by this repository.ID- the type of the primary key of the entity.
- All Superinterfaces:
Repository
EntityRepository relies on preview features of the Java platform:
EntityRepositoryrefers to one or more preview APIs:StringTemplate.
Using Entity Repositories
Entity repositories provide a high-level abstraction for managing entities in the database. They offer a set of
methods for creating, reading, updating, and deleting entities, as well as querying and filtering entities based on
specific criteria. The EntityRepository interface is designed to work with entity records that implement the
Entity interface, providing a consistent and type-safe way to interact with the database.
Entity Definition
Define the entity records to use them to in combination with repositories. The Entity interface is a
marker interface that indicates that the record is an entity and has a primary key of type ID. The PK
annotation is used to mark the primary key field of the entity record. The FK annotation is used to mark
the foreign key field of the entity record. The Inline annotation (optional) is used to mark the record
component that is inlined in the entity record.
Example:
record City(@PK int id,
String name,
long population
) implements Entity<Integer> {};
record Address(String street, String postalCode, @FK City city)
record User(@PK int id,
String email,
LocalDate birthDate,
@Inline Address address
) implements Entity<Integer> {};
Repository Lookup
An entity repository can be obtained by invoking entity on an ORMTemplate with the desired entity
class. The orm template can be requested as demonstrated below. Note that orm templates are supported for
Data Sources, JDBC Connections and JPA Entity Managers.
ORMTemplate orm = Templates.ORM(dataSource);
EntityRepository<User> userRepository = orm.entity(User.class);
Alternatively, a specialized repository can be requested by calling the repository method with the repository
class. Specialized repositories allow specialized repository methods to be defined in the repository interface. The
specialized repository can be used to implement specialized queries or operations that are specific to the entity type.
The specialized logic can utilize the QueryBuilder interface to build SELECT and DELETE statements.
interface UserRepository extends EntityRepository<User> {
// Specialized repository methods go here:
default Optional<User> findByEmail(String email) {
return select()
.where(User_.email, EQUALS, email)
.getOptionalResult();
}
}
UserRepository userRepository = orm.repository(UserRepository.class)
Repository Injection
A specialized repository can also be injected using Spring's dependency injection mechanism when the
storm-spring package is included in the project. Check the storm-spring package to lean how to make
repositories available to the application for dependency injection.
CRUD Operations
Entity repositories provide a set of methods for creating, reading, updating, and deleting entities in the database. The following sections provide examples of how to use these methods to interact with the database.
Create
Insert a user into the database. The template engine also supports insertion of multiple entries in batch mode by passing a list of entities. Alternatively, insertion can also be executed using a stream of entities.
User user = ...;
userRepository.insert(user);
Read
Select all users from the database that are linked to cities with the name "Sunnyvale". The static metamodel is used to specify the City entity in the QueryBuilder's entity graph.
List<City> cities = cityRepository.findByName("Sunnyvale")
List<User> users = userRepository
.select()
.where(User_.address.city, cities) // Type-safe metamodel.
.getResultList();
Alternatively, getResultStream() can be invoked to load the users lazily.
The QueryBuilder also allows the previous queries to be combined into a single select query, using the User's static metamodel to specify the city name field in the QueryBuilder's entity graph.
List<User> users = userRepository
.select()
.where(User_.address.city.name, EQUALS, "Sunnyvale") // Type-safe metamodel.
.getResultList();
Update
Update a user in the database. The repository also supports updates for multiple entries in batch model by passing a list of entities. Alternatively, updates can also be executed using a stream of entities.
User user = ...;
userRepository.update(user);
Delete
Remove user from the database. The repository also supports removals for multiple entries in batch mode by passing a list entities or primary keys. Alternatively, removal can be executed using a stream of entities.
User user = ...;
userRepository.remove(user);
Also here, the QueryBuilder can be used to create specialized statement, for instance, to delete all users where the email field IS NULL.
repository
.delete()
.where(User_.email, IS_NULL) // Type-safe metamodel.
.executeUpdate();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongcount()Returns the number of entities in the database of the entity type supported by this repository.default <V extends Data>
longCounts entities matching the specified field and referenced value.default <V> longCounts entities matching the specified field and value.longCounts the number of entities identified by the provided stream of IDs using the default batch size.longCounts the number of entities identified by the provided stream of IDs, with the counting process divided into batches of the specified size.longcountByRef(Stream<Ref<E>> refs) Counts the number of entities identified by the provided stream of refs using the default batch size.longcountByRef(Stream<Ref<E>> refs, int chunkSize) Counts the number of entities identified by the provided stream of refs, with the counting process divided into batches of the specified size.QueryBuilder<E, ?, ID> delete()Creates a new query builder for delete entities of the type managed by this repository.booleanexists()Checks if any entity of the type managed by this repository exists in the database.default <V extends Data>
booleanChecks if any entity matching the specified field and referenced value exists.default <V> booleanChecks if any entity matching the specified field and value exists.booleanexistsById(ID id) Checks if an entity with the specified primary key exists in the database.booleanexistsByRef(Ref<E> ref) Checks if an entity with the specified primary key exists in the database.findAll()Returns a list of all entities of the type supported by this repository.Retrieves entities matching a single field against multiple values.Retrieves entities matching a single field and a single referenced value.Retrieves entities matching a single field and a single value.findAllById(Iterable<ID> ids) Retrieves a list of entities based on their primary keys.findAllByRef(Iterable<Ref<E>> refs) Retrieves a list of entities based on their primary keys.findAllByRef(Metamodel<E, V> field, Iterable<? extends Ref<V>> values) Retrieves entities matching a single field against multiple referenced values.Returns a list of refs to all entities of the type supported by this repository.findAllRefBy(Metamodel<E, V> field, Iterable<? extends V> values) Retrieves refs to entities matching a single field against multiple values.findAllRefBy(Metamodel<E, V> field, Ref<V> value) Retrieves refs to entities matching a single field and a single referenced value.findAllRefBy(Metamodel<E, V> field, V value) Retrieves refs to entities matching a single field and a single value.findAllRefByRef(Metamodel<E, V> field, Iterable<? extends Ref<V>> values) Retrieves refs to entities matching a single field against multiple referenced values.findBy(Metamodel.Key<E, V> key, V value) Retrieves an entity by the value of a unique key field.Retrieves an entity based on a single field and its referenced value.Retrieves an entity based on a single field and its value.Retrieves an entity based on its primary key.findByRef(Metamodel.Key<E, V> key, Ref<V> value) Retrieves an entity by the ref value of a unique key field that references another entity.Retrieves an entity based on its primary key, expressed by a ref.Retrieves a ref to an entity based on a single field and its referenced value.Retrieves a ref to an entity based on a single field and its value.<V> EgetBy(Metamodel.Key<E, V> key, V value) Retrieves an entity by the value of a unique key field.Retrieves exactly one entity based on a single field and its referenced value.default <V> ERetrieves exactly one entity based on a single field and its value.Retrieves an entity based on its primary key.getByRef(Metamodel.Key<E, V> key, Ref<V> value) Retrieves an entity by the ref value of a unique key field that references another entity.Retrieves an entity based on its primary key, expressed by a ref.Retrieves a ref to exactly one entity based on a single field and its referenced value.Retrieves a ref to exactly one entity based on a single field and its value.voidInserts an entity into the database.voidInserts an entity into the database.voidInserts a collection of entities into the database in batches.voidInserts a collection of entities into the database in batches.voidInserts entities in a batch mode to optimize performance and reduce database load.voidInserts entities in a batch mode to optimize performance and reduce database load.voidInserts a stream of entities into the database, with the insertion process divided into batches of the specified size.voidInserts a stream of entities into the database, with the insertion process divided into batches of the specified size.insertAndFetch(E entity) Inserts a single entity into the database and returns the inserted entity with its current state.insertAndFetch(Iterable<E> entities) Inserts a collection of entities into the database in batches.insertAndFetchId(E entity) Inserts an entity into the database and returns its primary key.insertAndFetchIds(Iterable<E> entities) Inserts a collection of entities into the database in batches.model()Returns the entity model associated with this repository.page(int pageNumber, int pageSize) Returns a page of entities using offset-based pagination.Returns a page of entities using offset-based pagination.pageRef(int pageNumber, int pageSize) Returns a page of entity refs using offset-based pagination.Returns a page of entity refs using offset-based pagination.Creates a new ref entity instance for the specified entity.Creates a new ref entity instance with the specified primary key.voidRemoves an entity from the database.voidRemoves a collection of entities from the database in batches.voidRemoves a stream of entities from the database in batches.voidRemoves a stream of entities from the database in configurable batch sizes.voidRemoves all entities from the database.default <V> intremoveAllBy(Metamodel<E, V> field, Iterable<? extends V> values) Removes entities matching the specified field against multiple values.default <V extends Data>
intremoveAllBy(Metamodel<E, V> field, Ref<V> value) Removes entities matching the specified field and referenced value.default <V> intremoveAllBy(Metamodel<E, V> field, V value) Removes entities matching the specified field and value.default <V extends Data>
intremoveAllByRef(Metamodel<E, V> field, Iterable<? extends Ref<V>> values) Removes entities matching the specified field against multiple referenced values.voidremoveById(ID id) Removes an entity from the database based on its primary key.voidremoveByRef(Iterable<Ref<E>> refs) Removes a collection of entities from the database in batches.voidremoveByRef(Stream<Ref<E>> refs) Removes a stream of entities from the database in batches.voidremoveByRef(Stream<Ref<E>> refs, int batchSize) Removes a stream of entities from the database in configurable batch sizes.voidremoveByRef(Ref<E> ref) Removes an entity from the database by its reference.scroll(Scrollable<E> scrollable) Executes a scroll request from aScrollabletoken, typically obtained fromWindow.next()orWindow.previous().QueryBuilder<E, E, ID> select()Creates a new query builder for selecting entities of the type managed by this repository.<R> QueryBuilder<E, R, ID> Creates a new query builder for the specializedselectType.<R> QueryBuilder<E, R, ID> select(Class<R> selectType, StringTemplatePREVIEW template) Creates a new query builder for the specializedselectTypeand specializedtemplatefor the select clause.Creates a new query builder for the entity type managed by this repository.QueryBuilder<E, Ref<E>, ID> Creates a new query builder for selecting refs to entities of the type managed by this repository.<R extends Data>
QueryBuilder<E, Ref<R>, ID> Creates a new query builder for selecting refs to entities of the type managed by this repository.Unloads the given entity from memory by converting it into a lightweight ref containing only its primary key.voidUpdates a single entity in the database.voidUpdates a collection of entities in the database in batches.voidUpdates a stream of entities in the database using the default batch size.voidUpdates a stream of entities in the database, with the update process divided into batches of the specified size.updateAndFetch(E entity) Updates a single entity in the database and returns the updated entity with its current state.updateAndFetch(Iterable<E> entities) Updates a collection of entities in the database in batches and returns a list of the updated entities.voidInserts or updates a single entity in the database.voidInserts or updates a collection of entities in the database in batches.voidInserts or updates a stream of entities in the database in batches.voidInserts or updates a stream of entities in the database in configurable batch sizes.upsertAndFetch(E entity) Inserts or updates a single entity in the database and returns the entity with its current state.upsertAndFetch(Iterable<E> entities) Inserts or updates a collection of entities in the database in batches and returns a list of the upserted entities.upsertAndFetchId(E entity) Inserts or updates a single entity in the database and returns its ID.upsertAndFetchIds(Iterable<E> entities) Inserts or updates a collection of entities in the database in batches and returns a list of their IDs.Methods inherited from interface st.orm.repository.Repository
orm
-
Method Details
-
model
Returns the entity model associated with this repository.- Returns:
- the entity model.
-
ref
Creates a new ref entity instance with the specified primary key.This method creates a lightweight reference that encapsulates only the primary key of an entity, without loading the full entity data into memory. The complete record can be fetched on demand by invoking
Ref.fetch(), which will trigger a separate database call.- Parameters:
id- the primary key of the entity.- Returns:
- a ref entity instance containing only the primary key.
- Since:
- 1.3
-
ref
Creates a new ref entity instance for the specified entity.This method wraps a fully loaded entity in a lightweight reference. Although the complete entity is provided, the returned ref retains only the primary key for identification. In this case, calling
Ref.fetch()will return the full entity (which is already loaded), ensuring a consistent API for accessing entity records on demand. This approach supports lazy-loading scenarios where only the identifier is needed initially.- Parameters:
entity- the entity to wrap in a ref.- Returns:
- a ref entity instance containing the primary key of the provided entity.
- Since:
- 1.3
-
unload
Unloads the given entity from memory by converting it into a lightweight ref containing only its primary key.This method discards the full entity data and returns an attached ref that encapsulates just the primary key. The actual record is not retained in memory, but can be retrieved on demand by calling
Ref.fetch(), which will trigger a new database call. This approach is particularly useful when you need to minimize memory usage while keeping the option to re-fetch the complete record later.Unlike
Ref.unload(), which returns a detached ref, this method returns an attached ref that can re-fetch the entity from the database.- Parameters:
entity- the entity to unload into a lightweight ref.- Returns:
- an attached ref containing only the primary key of the entity, allowing the full record to be fetched again when needed.
- Since:
- 1.3
-
select
QueryBuilder<E,E, select()ID> Creates a new query builder for selecting entities of the type managed by this repository.- Returns:
- a new query builder for the entity type.
-
selectCount
QueryBuilder<E,Long, selectCount()ID> Creates a new query builder for the entity type managed by this repository.- Returns:
- a new query builder for the entity type.
-
select
Creates a new query builder for the specializedselectType.- Type Parameters:
R- the result type of the query.- Parameters:
selectType- the result type of the query.- Returns:
- a new query builder for the specialized
selectType.
-
selectRef
QueryBuilder<E,Ref<E>, selectRef()ID> Creates a new query builder for selecting refs to entities of the type managed by this repository.This method is typically used when you only need the primary keys of the entities initially, and you want to defer fetching the full data until it is actually required. The query builder will return ref instances that encapsulate the primary key. To retrieve the full entity, call
Ref.fetch(), which will perform an additional database query on demand.- Returns:
- a new query builder for selecting refs to entities.
- Since:
- 1.3
-
select
<R> QueryBuilder<E,R, selectID> (@Nonnull Class<R> selectType, @Nonnull StringTemplatePREVIEW template) Creates a new query builder for the specializedselectTypeand specializedtemplatefor the select clause.- Type Parameters:
R- the result type of the query.- Parameters:
selectType- the result type of the query.template- the specialized template for the select clause.- Returns:
- a new query builder for the specialized
selectType.
-
selectRef
Creates a new query builder for selecting refs to entities of the type managed by this repository.This method is typically used when you only need the primary keys of the entities initially, and you want to defer fetching the full data until it is actually required. The query builder will return ref instances that encapsulate the primary key. To retrieve the full entity, call
Ref.fetch(), which will perform an additional database query on demand.- Parameters:
refType- the type that is selected as ref.- Returns:
- a new query builder for selecting refs to entities.
- Since:
- 1.3
-
delete
QueryBuilder<E,?, delete()ID> Creates a new query builder for delete entities of the type managed by this repository.- Returns:
- a new query builder for the entity type.
-
count
long count()Returns the number of entities in the database of the entity type supported by this repository.- Returns:
- the total number of entities in the database as a long value.
- Throws:
PersistenceException- if the count operation fails due to underlying database issues, such as connectivity.
-
exists
boolean exists()Checks if any entity of the type managed by this repository exists in the database.- Returns:
- true if at least one entity exists, false otherwise.
- Throws:
PersistenceException- if there is an underlying database issue during the count operation.
-
existsById
Checks if an entity with the specified primary key exists in the database.This method determines the presence of an entity by checking if the count of entities with the given primary key is greater than zero. It leverages the
selectCountmethod, which performs a count operation on the database.- Parameters:
id- the primary key of the entity to check for existence.- Returns:
- true if an entity with the specified primary key exists, false otherwise.
- Throws:
PersistenceException- if there is an underlying database issue during the count operation.
-
existsByRef
Checks if an entity with the specified primary key exists in the database.This method determines the presence of an entity by checking if the count of entities with the given primary key is greater than zero. It leverages the
selectCountmethod, which performs a count operation on the database.- Parameters:
ref- the primary key of the entity to check for existence, expressed as a ref.- Returns:
- true if an entity with the specified primary key exists, false otherwise.
- Throws:
PersistenceException- if there is an underlying database issue during the count operation.
-
insert
Inserts an entity into the database.This method adds a new entity to the database. It ensures that the entity is persisted according to the defined database constraints and entity model. It's critical for the entity to be fully initialized as per the entity model requirements.
- Parameters:
entity- the entity to insert. The entity must satisfy all model constraints.- Throws:
PersistenceException- if the insert operation fails. This can happen due to a variety of reasons, including database constraints violations, connectivity issues, or if the entity parameter is null.
-
insert
Inserts an entity into the database.This method adds a new entity to the database. It ensures that the entity is persisted according to the defined database constraints and entity model. It's critical for the entity to be fully initialized as per the entity model requirements.
- Parameters:
entity- the entity to insert. The entity must satisfy all model constraints.ignoreAutoGenerate- true to ignore the auto-generate flag on the primary key and explicitly insert the provided primary key value. Use this flag only when intentionally providing the primary key value (e.g., migrations, data exports).- Throws:
PersistenceException- if the insert operation fails. This can happen due to a variety of reasons, including database constraints violations, connectivity issues, or if the entity parameter is null.
-
insertAndFetchId
Inserts an entity into the database and returns its primary key.This method adds a new entity to the database and upon successful insertion, returns the primary key assigned to the entity when the primary key is generated by the database (e.g., auto-incremented). Otherwise, if the primary key is not generated by the database, the method returns an empty optional.
- Parameters:
entity- the entity to insert. The entity must satisfy all model constraints.- Returns:
- the generated primary key of the successfully inserted entity.
- Throws:
PersistenceException- if the insert operation fails for reasons such as database constraints violations, connectivity issues, or if the entity parameter is null.
-
insertAndFetch
Inserts a single entity into the database and returns the inserted entity with its current state.This method inserts the provided entity into the database. Upon successful insertion, it returns the entity as it exists in the database after the operation. This ensures that the returned entity includes any modifications applied during the insertion process, such as generated primary keys, default values, or other automatic changes triggered by the database.
- Parameters:
entity- the entity to be inserted. The entity must be non-null and contain valid data for insertion into the database.- Returns:
- the inserted entity, reflecting its state in the database after insertion. This includes any database-applied changes such as primary key assignments or default values.
- Throws:
PersistenceException- if the insertion operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
update
Updates a single entity in the database.This method updates the provided entity in the database, modifying its existing record to reflect the current state of the entity. It is intended for cases where only one entity needs to be updated.
- Parameters:
entity- the entity to be updated. The entity must be non-null and contain valid data for updating its corresponding record in the database.- Throws:
PersistenceException- if the update operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
updateAndFetch
Updates a single entity in the database and returns the updated entity with its current state.This method updates the provided entity in the database and, upon successful completion, returns the entity as it exists in the database after the update operation. This ensures that the returned entity reflects any modifications applied during the update process, such as updated timestamps, versioning, or other automatic changes triggered by the database.
- Parameters:
entity- the entity to be updated. The entity must be non-null and contain valid data for updating its corresponding record in the database.- Returns:
- the updated entity, reflecting its state in the database after the update. This includes any database-applied changes such as modified timestamps or version numbers.
- Throws:
PersistenceException- if the update operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsert
Inserts or updates a single entity in the database.This method performs an "upsert" operation on the provided entity. If the entity does not already exist in the database, it will be inserted. If it does exist, it will be updated to reflect the current state of the entity. This approach ensures that the entity is either created or brought up-to-date, depending on its existence in the database.
- Parameters:
entity- the entity to be inserted or updated. The entity must be non-null and contain valid data for insertion or update in the database.- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsertAndFetchId
Inserts or updates a single entity in the database and returns its ID.This method performs an "upsert" operation on the provided entity. If the entity does not already exist in the database, it will be inserted; if it exists, it will be updated. Upon successful completion, the method returns the ID of the entity as stored in the database. This approach ensures that the entity is either created or brought up-to-date, depending on its existence in the database.
- Parameters:
entity- the entity to be inserted or updated. The entity must be non-null and contain valid data for insertion or update in the database.- Returns:
- the ID of the upserted entity, reflecting its identifier in the database.
- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsertAndFetch
Inserts or updates a single entity in the database and returns the entity with its current state.This method performs an "upsert" operation on the provided entity. If the entity does not already exist in the database, it will be inserted; if it exists, it will be updated. Upon successful completion, the method returns the entity as it exists in the database after the upsert operation. This ensures that the returned entity reflects any modifications applied during the upsert process, such as generated primary keys, updated timestamps, or default values set by the database.
- Parameters:
entity- the entity to be inserted or updated. The entity must be non-null and contain valid data for insertion or update in the database.- Returns:
- the upserted entity, reflecting its current state in the database. This includes any database-applied changes, such as primary key assignments, default values, or timestamp updates.
- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
remove
Removes an entity from the database.This method removes an existing entity from the database. The entity must exist in the database; if it does not, a
PersistenceExceptionis thrown. UnlikeremoveById(ID)andremoveByRef(st.orm.Ref<E>), this method is strict rather than idempotent, because possessing the full entity implies the caller expects it to exist.- Parameters:
entity- the entity to remove. The entity must exist in the database and should be correctly identified by its primary key.- Throws:
PersistenceException- if the removal operation fails. Reasons for failure might include the entity not being found in the database, violations of database constraints, connectivity issues, or if the entity parameter is null.
-
removeById
Removes an entity from the database based on its primary key.This method ensures the entity with the given primary key is removed from the database. If the entity does not exist, the operation completes successfully without error (idempotent behavior).
- Parameters:
id- the primary key of the entity to remove.- Throws:
PersistenceException- if the removal operation fails due to violations of database constraints, connectivity issues, or if the id parameter is null.
-
removeByRef
Removes an entity from the database by its reference.This method ensures the entity identified by the given reference is removed from the database. If the entity does not exist, the operation completes successfully without error (idempotent behavior).
- Parameters:
ref- the reference to the entity to remove.- Throws:
PersistenceException- if the removal operation fails due to violations of database constraints, connectivity issues, or if the ref parameter is null.
-
removeAll
void removeAll()Removes all entities from the database.This method performs a bulk removal operation, removing all instances of the entities managed by this repository from the database.
- Throws:
PersistenceException- if the bulk removal operation fails. Failure can occur for several reasons, including but not limited to database access issues, transaction failures, or underlying database constraints that prevent the removal of certain records.
-
findById
Retrieves an entity based on its primary key.This method performs a lookup in the database, returning the corresponding entity if it exists.
- Parameters:
id- the primary key of the entity to retrieve.- Returns:
- the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
-
findByRef
Retrieves an entity based on its primary key, expressed by a ref.This method performs a lookup in the database, returning the corresponding entity if it exists.
- Parameters:
ref- the ref to match.- Returns:
- the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
-
getById
Retrieves an entity based on its primary key.This method performs a lookup in the database, returning the corresponding entity if it exists.
- Parameters:
id- the primary key of the entity to retrieve.- Returns:
- the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
- Throws:
NoResultException- if no entity is found matching the given primary key, indicating that there's no corresponding data in the database.PersistenceException- if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
-
getByRef
Retrieves an entity based on its primary key, expressed by a ref.This method performs a lookup in the database, returning the corresponding entity if it exists.
- Parameters:
ref- the ref to match.- Returns:
- the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
- Throws:
NoResultException- if no entity is found matching the given primary key, indicating that there's no corresponding data in the database.PersistenceException- if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
-
findBy
Retrieves an entity by the value of a unique key field.- Type Parameters:
V- the type of the key field.- Parameters:
key- the metamodel key identifying a unique column.value- the value to match.- Returns:
- the entity matching the given key value, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.9
-
getBy
Retrieves an entity by the value of a unique key field.- Type Parameters:
V- the type of the key field.- Parameters:
key- the metamodel key identifying a unique column.value- the value to match.- Returns:
- the entity matching the given key value.
- Throws:
NoResultException- if no entity is found matching the given key value.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.9
-
findByRef
Retrieves an entity by the ref value of a unique key field that references another entity.- Type Parameters:
V- the type of the referenced entity.- Parameters:
key- the metamodel key identifying a unique foreign key column.value- the ref value to match.- Returns:
- the entity matching the given ref value, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.9
-
getByRef
Retrieves an entity by the ref value of a unique key field that references another entity.- Type Parameters:
V- the type of the referenced entity.- Parameters:
key- the metamodel key identifying a unique foreign key column.value- the ref value to match.- Returns:
- the entity matching the given ref value.
- Throws:
NoResultException- if no entity is found matching the given ref value.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.9
-
findBy
Retrieves an entity based on a single field and its value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- the entity matching the given field value, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findBy
Retrieves an entity based on a single field and its referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- the entity matching the given ref value, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllBy
Retrieves entities matching a single field and a single value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- a list of matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllBy
Retrieves entities matching a single field and a single referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- a list of matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllBy
Retrieves entities matching a single field against multiple values.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.values- the values to match against.- Returns:
- a list of matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllByRef
default <V extends Data> List<E> findAllByRef(@Nonnull Metamodel<E, V> field, @Nonnull Iterable<? extends Ref<V>> values) Retrieves entities matching a single field against multiple referenced values.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.values- the referenced values to match against.- Returns:
- a list of matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
getBy
Retrieves exactly one entity based on a single field and its value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- the matching entity.
- Throws:
NoResultException- if there is no result.NonUniqueResultException- if more than one result.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
getBy
Retrieves exactly one entity based on a single field and its referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- the matching entity.
- Throws:
NoResultException- if there is no result.NonUniqueResultException- if more than one result.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findRefBy
Retrieves a ref to an entity based on a single field and its value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- a ref to the matching entity, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findRefBy
default <V extends Data> Optional<Ref<E>> findRefBy(@Nonnull Metamodel<E, V> field, @Nonnull Ref<V> value) Retrieves a ref to an entity based on a single field and its referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- a ref to the matching entity, or empty if none exists.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllRefBy
Retrieves refs to entities matching a single field and a single value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- a list of refs to matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllRefBy
default <V extends Data> List<Ref<E>> findAllRefBy(@Nonnull Metamodel<E, V> field, @Nonnull Ref<V> value) Retrieves refs to entities matching a single field and a single referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- a list of refs to matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllRefBy
default <V> List<Ref<E>> findAllRefBy(@Nonnull Metamodel<E, V> field, @Nonnull Iterable<? extends V> values) Retrieves refs to entities matching a single field against multiple values.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.values- the values to match against.- Returns:
- a list of refs to matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
findAllRefByRef
default <V extends Data> List<Ref<E>> findAllRefByRef(@Nonnull Metamodel<E, V> field, @Nonnull Iterable<? extends Ref<V>> values) Retrieves refs to entities matching a single field against multiple referenced values.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.values- the referenced values to match against.- Returns:
- a list of refs to matching entities, or an empty list if none found.
- Throws:
PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
getRefBy
Retrieves a ref to exactly one entity based on a single field and its value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- a ref to the matching entity.
- Throws:
NoResultException- if there is no result.NonUniqueResultException- if more than one result.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
getRefBy
Retrieves a ref to exactly one entity based on a single field and its referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- a ref to the matching entity.
- Throws:
NoResultException- if there is no result.NonUniqueResultException- if more than one result.PersistenceException- if the retrieval operation fails due to underlying database issues.- Since:
- 1.11
-
countBy
Counts entities matching the specified field and value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- the count of matching entities.
- Throws:
PersistenceException- if the count operation fails due to underlying database issues.- Since:
- 1.11
-
countBy
Counts entities matching the specified field and referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- the count of matching entities.
- Throws:
PersistenceException- if the count operation fails due to underlying database issues.- Since:
- 1.11
-
existsBy
Checks if any entity matching the specified field and value exists.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- true if any matching entities exist, false otherwise.
- Throws:
PersistenceException- if the count operation fails due to underlying database issues.- Since:
- 1.11
-
existsBy
Checks if any entity matching the specified field and referenced value exists.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- true if any matching entities exist, false otherwise.
- Throws:
PersistenceException- if the count operation fails due to underlying database issues.- Since:
- 1.11
-
removeAllBy
Removes entities matching the specified field and value.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.value- the value to match against.- Returns:
- the number of entities removed.
- Throws:
PersistenceException- if the removal operation fails due to underlying database issues.- Since:
- 1.11
-
removeAllBy
Removes entities matching the specified field and referenced value.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.value- the referenced value to match against.- Returns:
- the number of entities removed.
- Throws:
PersistenceException- if the removal operation fails due to underlying database issues.- Since:
- 1.11
-
removeAllBy
Removes entities matching the specified field against multiple values.- Type Parameters:
V- the type of the field.- Parameters:
field- metamodel reference of the entity field.values- the values to match against.- Returns:
- the number of entities removed.
- Throws:
PersistenceException- if the removal operation fails due to underlying database issues.- Since:
- 1.11
-
removeAllByRef
default <V extends Data> int removeAllByRef(@Nonnull Metamodel<E, V> field, @Nonnull Iterable<? extends Ref<V>> values) Removes entities matching the specified field against multiple referenced values.- Type Parameters:
V- the type of the referenced entity.- Parameters:
field- metamodel reference of the entity field.values- the referenced values to match against.- Returns:
- the number of entities removed.
- Throws:
PersistenceException- if the removal operation fails due to underlying database issues.- Since:
- 1.11
-
page
Returns a page of entities using offset-based pagination.This method executes two queries: a
SELECT COUNT(*)to determine the total number of entities, and a query with OFFSET and LIMIT to fetch the content for the requested page.Page numbers are zero-based: pass
0for the first page.- Parameters:
pageNumber- the zero-based page index.pageSize- the maximum number of entities per page.- Returns:
- a page containing the results and pagination metadata.
- Since:
- 1.10
-
page
Returns a page of entities using offset-based pagination.This method executes two queries: a
SELECT COUNT(*)to determine the total number of entities, and a query with OFFSET and LIMIT to fetch the content for the requested page.Use
Pageable.ofSize(int)for the first page, then navigate withPage.nextPageable()orPage.previousPageable().- Parameters:
pageable- the pagination request specifying page number and page size.- Returns:
- a page containing the results and pagination metadata.
- Since:
- 1.10
-
pageRef
Returns a page of entity refs using offset-based pagination.Page numbers are zero-based: pass
0for the first page.- Parameters:
pageNumber- the zero-based page index.pageSize- the maximum number of refs per page.- Returns:
- a page containing the ref results and pagination metadata.
- Since:
- 1.10
-
pageRef
Returns a page of entity refs using offset-based pagination.This method executes two queries: a
SELECT COUNT(*)to determine the total number of entities, and a query with OFFSET and LIMIT to fetch the refs for the requested page.- Parameters:
pageable- the pagination request specifying page number and page size.- Returns:
- a page containing the ref results and pagination metadata.
- Since:
- 1.10
-
scroll
Executes a scroll request from aScrollabletoken, typically obtained fromWindow.next()orWindow.previous().- Parameters:
scrollable- the scroll request containing cursor state, key, sort, size, and direction.- Returns:
- a window containing the results and navigation tokens.
- Since:
- 1.11
-
findAll
Returns a list of all entities of the type supported by this repository. Each element in the list represents an entity in the database, encapsulating all relevant data as mapped by the entity model.Note: Loading all entities into memory at once can be very memory-intensive if your table is large.
- Returns:
- a stream of all entities of the type supported by this repository.
- Throws:
PersistenceException- if the selection operation fails due to underlying database issues, such as connectivity.
-
findAllRef
Returns a list of refs to all entities of the type supported by this repository. Each element in the list represents a lightweight reference to an entity in the database, containing only the primary key.This method is useful when you need to retrieve all entity identifiers without loading the full entity data. The complete entity can be fetched on demand by calling
Ref.fetch()on any of the returned refs.Note: While this method is more memory-efficient than
findAll()since it only loads primary keys, loading all refs into memory at once can still be memory-intensive for very large tables.- Returns:
- a list of refs to all entities of the type supported by this repository.
- Throws:
PersistenceException- if the selection operation fails due to underlying database issues, such as connectivity.- Since:
- 1.3
-
findAllById
Retrieves a list of entities based on their primary keys.This method retrieves entities matching the provided IDs in batches, consolidating them into a single list. The batch-based retrieval minimizes database overhead, allowing efficient handling of larger collections of IDs.
Note: The order of entities in the returned list is not guaranteed to match the order of IDs in the input collection, as the database may not preserve insertion order during retrieval.
- Parameters:
ids- the primary keys of the entities to retrieve, represented as an iterable collection.- Returns:
- a list of entities corresponding to the provided primary keys. Entities are returned without any guarantee of order alignment with the input list. If an ID does not correspond to any entity in the database, no corresponding entity will be included in the returned list.
- Throws:
PersistenceException- if the selection operation fails due to database issues, such as connectivity problems or invalid input parameters.
-
findAllByRef
Retrieves a list of entities based on their primary keys.This method retrieves entities matching the provided IDs in batches, consolidating them into a single list. The batch-based retrieval minimizes database overhead, allowing efficient handling of larger collections of IDs.
Note: The order of entities in the returned list is not guaranteed to match the order of IDs in the input collection, as the database may not preserve insertion order during retrieval.
- Parameters:
refs- the primary keys of the entities to retrieve, represented as an iterable collection.- Returns:
- a list of entities corresponding to the provided primary keys. Entities are returned without any guarantee of order alignment with the input list. If an ID does not correspond to any entity in the database, no corresponding entity will be included in the returned list.
- Throws:
PersistenceException- if the selection operation fails due to database issues, such as connectivity problems or invalid input parameters.
-
insert
Inserts a collection of entities into the database in batches.This method processes the provided entities in batches, optimizing insertion for larger collections by reducing database overhead. Batch processing helps ensure that even large numbers of entities can be inserted efficiently and minimizes potential memory and performance issues.
- Parameters:
entities- an iterable collection of entities to be inserted. Each entity in the collection must be non-null and contain valid data for insertion.- Throws:
PersistenceException- if the insertion operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
insert
Inserts a collection of entities into the database in batches.This method processes the provided entities in batches, optimizing insertion for larger collections by reducing database overhead. Batch processing helps ensure that even large numbers of entities can be inserted efficiently and minimizes potential memory and performance issues.
- Parameters:
entities- an iterable collection of entities to be inserted. Each entity in the collection must be non-null and contain valid data for insertion.ignoreAutoGenerate- true to ignore the auto-generate flag on the primary key and explicitly insert the provided primary key value. Use this flag only when intentionally providing the primary key value (e.g., migrations, data exports).- Throws:
PersistenceException- if the insertion operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
insertAndFetchIds
Inserts a collection of entities into the database in batches.This method processes the provided entities in batches, optimizing insertion for larger collections by reducing database overhead. Batch processing helps ensure that even large numbers of entities can be inserted efficiently and minimizes potential memory and performance issues.
Upon successful insertion, it returns the primary keys assigned to the entities when the primary keys are generated by the database (e.g., auto-incremented). Otherwise, if the primary keys are not generated by the database, the method returns an empty list.
- Parameters:
entities- an iterable collection of entities to be inserted. Each entity in the collection must be non-null and contain valid data for insertion.- Returns:
- the primary keys assigned to the entities when the primary keys are generated by the database,
- Throws:
PersistenceException- if the insertion operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
insertAndFetch
Inserts a collection of entities into the database in batches.This method processes the provided entities in batches, optimizing insertion for larger collections by reducing database overhead. Batch processing helps ensure that even large numbers of entities can be inserted efficiently and minimizes potential memory and performance issues.
Upon successful insertion, it returns the entities that were inserted. The returned entities reflect the state of the entities as they exist in the database after the insertion operation. This ensures that the returned entities include any changes that might have been applied during the insertion process, such as primary key, default values or triggers.
- Parameters:
entities- an iterable collection of entities to be inserted. Each entity in the collection must be non-null and contain valid data for insertion.- Returns:
- the entities that were inserted into the database.
- Throws:
PersistenceException- if the insertion operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
update
Updates a collection of entities in the database in batches.This method processes the provided entities in batches to optimize updating of larger collections, reducing database overhead and improving performance. Batch processing allows efficient handling of bulk updates, minimizing memory and processing costs.
- Parameters:
entities- an iterable collection of entities to be updated. Each entity in the collection must be non-null and contain valid, up-to-date data for modification in the database.- Throws:
PersistenceException- if the update operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
updateAndFetch
Updates a collection of entities in the database in batches and returns a list of the updated entities.This method processes the provided entities in batches, optimizing performance for larger collections by reducing database overhead. Upon successful update, it returns the entities as they exist in the database after the update operation. This ensures that the returned entities reflect any modifications applied during the update process, such as updated timestamps, versioning, or other automatic changes made by the database.
- Parameters:
entities- an iterable collection of entities to be updated. Each entity in the collection must be non-null and contain valid data for modification in the database.- Returns:
- a list of entities reflecting their state in the database after the update. The order of entities in the returned list is not guaranteed to match the order of the input collection.
- Throws:
PersistenceException- if the update operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsert
Inserts or updates a collection of entities in the database in batches.This method processes the provided entities in batches, optimizing performance for larger collections by reducing database overhead. For each entity, the method performs an "upsert" operation, meaning it will insert the entity if it does not already exist in the database, or update it if it does. This approach ensures that the entities are either created or brought up-to-date, depending on their existence in the database.
- Parameters:
entities- an iterable collection of entities to be inserted or updated. Each entity in the collection must be non-null and contain valid data for insertion or update in the database.- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsertAndFetchIds
Inserts or updates a collection of entities in the database in batches and returns a list of their IDs.This method processes the provided entities in batches to optimize performance for larger collections, reducing database overhead. For each entity, the method performs an "upsert" operation, inserting the entity if it does not already exist in the database, or updating it if it does. Upon successful completion, the method returns a list of the IDs of the upserted entities, reflecting their identifiers as stored in the database.
- Parameters:
entities- an iterable collection of entities to be inserted or updated. Each entity in the collection must be non-null and contain valid data for insertion or update in the database.- Returns:
- a list of IDs corresponding to the upserted entities. The order of IDs in the returned list is not guaranteed to match the order of the input collection.
- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsertAndFetch
Inserts or updates a collection of entities in the database in batches and returns a list of the upserted entities.This method processes the provided entities in batches, optimizing performance for larger collections by reducing database overhead. For each entity, it performs an "upsert" operation, inserting the entity if it does not already exist in the database, or updating it if it does. Upon successful completion, it returns the entities as they exist in the database after the operation. This ensures that the returned entities reflect any changes applied during the upsert process, such as generated primary keys, updated timestamps, or default values set by the database.
- Parameters:
entities- an iterable collection of entities to be inserted or updated. Each entity in the collection must be non-null and contain valid data for insertion or update in the database.- Returns:
- a list of upserted entities reflecting their current state in the database. The order of entities in the returned list is not guaranteed to match the order of the input collection.
- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
remove
Removes a collection of entities from the database in batches.This method processes the provided entities in batches to optimize performance when handling larger collections, reducing database overhead. For each entity in the collection, the method removes the corresponding record from the database, if it exists. Batch processing ensures efficient handling of removals, particularly for large data sets.
- Parameters:
entities- an iterable collection of entities to be removed. Each entity in the collection must be non-null and represent a valid database record for removal.- Throws:
PersistenceException- if the removal operation fails due to database issues, such as connectivity problems or constraints violations.
-
removeByRef
Removes a collection of entities from the database in batches.This method processes the provided entities in batches to optimize performance when handling larger collections, reducing database overhead. For each entity in the collection, the method removes the corresponding record from the database, if it exists. Batch processing ensures efficient handling of removals, particularly for large data sets.
- Parameters:
refs- an iterable collection of entities to be removed. Each entity in the collection must be non-null and represent a valid database record for removal.- Throws:
PersistenceException- if the removal operation fails due to database issues, such as connectivity problems or constraints violations.
-
countById
Counts the number of entities identified by the provided stream of IDs using the default batch size.This method calculates the total number of entities that match the provided primary keys. The counting is performed in batches, which helps optimize performance and manage database load when dealing with large sets of IDs.
- Parameters:
ids- a stream of IDs for which to count matching entities.- Returns:
- the total count of entities matching the provided IDs.
- Throws:
PersistenceException- if there is an error during the counting operation, such as connectivity issues.
-
countById
Counts the number of entities identified by the provided stream of IDs, with the counting process divided into batches of the specified size.This method performs the counting operation in batches, specified by the
batchSizeparameter. This batching approach is particularly useful for efficiently handling large volumes of IDs, reducing the overhead on the database and improving performance.- Parameters:
ids- a stream of IDs for which to count matching entities.chunkSize- the size of the batches to use for the counting operation. A larger batch size can improve performance but may also increase the load on the database.- Returns:
- the total count of entities matching the provided IDs.
- Throws:
PersistenceException- if there is an error during the counting operation, such as connectivity issues.
-
countByRef
Counts the number of entities identified by the provided stream of refs using the default batch size.This method calculates the total number of entities that match the provided primary keys. The counting is performed in batches, which helps optimize performance and manage database load when dealing with large sets of IDs.
- Parameters:
refs- a stream of IDs for which to count matching entities.- Returns:
- the total count of entities matching the provided IDs.
- Throws:
PersistenceException- if there is an error during the counting operation, such as connectivity issues.
-
countByRef
Counts the number of entities identified by the provided stream of refs, with the counting process divided into batches of the specified size.This method performs the counting operation in batches, specified by the
batchSizeparameter. This batching approach is particularly useful for efficiently handling large volumes of IDs, reducing the overhead on the database and improving performance.- Parameters:
refs- a stream of IDs for which to count matching entities.chunkSize- the size of the batches to use for the counting operation. A larger batch size can improve performance but may also increase the load on the database.- Returns:
- the total count of entities matching the provided IDs.
- Throws:
PersistenceException- if there is an error during the counting operation, such as connectivity issues.
-
insert
Inserts entities in a batch mode to optimize performance and reduce database load.For large volumes of entities, this method processes the inserts in multiple batches to ensure efficient handling and minimize the impact on database resources. This structured approach facilitates the management of large-scale insert operations.
- Parameters:
entities- the entities to insert. Must not be null.- Throws:
PersistenceException- if the insert fails due to database constraints, connectivity issues, or if the entities parameter is null.
-
insert
Inserts entities in a batch mode to optimize performance and reduce database load.For large volumes of entities, this method processes the inserts in multiple batches to ensure efficient handling and minimize the impact on database resources. This structured approach facilitates the management of large-scale insert operations.
- Parameters:
entities- the entities to insert. Must not be null.ignoreAutoGenerate- true to ignore the auto-generate flag on the primary key and explicitly insert the provided primary key value. Use this flag only when intentionally providing the primary key value (e.g., migrations, data exports).- Throws:
PersistenceException- if the insert fails due to database constraints, connectivity issues, or if the entities parameter is null.
-
insert
Inserts a stream of entities into the database, with the insertion process divided into batches of the specified size.This method inserts entities provided in a stream and uses the specified batch size for the insertion operation. Batching the inserts can greatly enhance performance by minimizing the number of database interactions, especially useful when dealing with large volumes of data.
- Parameters:
entities- a stream of entities to insert. Each entity must not be null and must conform to the model constraints.batchSize- the size of the batches to use for the insertion operation. A larger batch size can improve performance but may also increase the load on the database.- Throws:
PersistenceException- if there is an error during the insertion operation, such as a violation of database constraints, connectivity issues, or if any entity in the stream is null.
-
insert
Inserts a stream of entities into the database, with the insertion process divided into batches of the specified size.This method inserts entities provided in a stream and uses the specified batch size for the insertion operation. Batching the inserts can greatly enhance performance by minimizing the number of database interactions, especially useful when dealing with large volumes of data.
- Parameters:
entities- a stream of entities to insert. Each entity must not be null and must conform to the model constraints.batchSize- the size of the batches to use for the insertion operation. A larger batch size can improve performance but may also increase the load on the database.ignoreAutoGenerate- true to ignore the auto-generate flag on the primary key and explicitly insert the provided primary key value. Use this flag only when intentionally providing the primary key value (e.g., migrations, data exports).- Throws:
PersistenceException- if there is an error during the insertion operation, such as a violation of database constraints, connectivity issues, or if any entity in the stream is null.
-
update
Updates a stream of entities in the database using the default batch size.This method updates entities provided in a stream, optimizing the update process by batching them with a default size. This helps to reduce the number of database operations and can significantly improve performance when updating large numbers of entities.
- Parameters:
entities- a stream of entities to update. Each entity must not be null, must already exist in the database, and must conform to the model constraints.- Throws:
PersistenceException- if there is an error during the update operation, such as a violation of database constraints, connectivity issues, or if any entity in the stream is null.
-
update
Updates a stream of entities in the database, with the update process divided into batches of the specified size.This method updates entities provided in a stream and uses the specified batch size for the update operation. Batching the updates can greatly enhance performance by minimizing the number of database interactions, especially useful when dealing with large volumes of data.
- Parameters:
entities- a stream of entities to update. Each entity must not be null, must already exist in the database, and must conform to the model constraints.batchSize- the size of the batches to use for the update operation. A larger batch size can improve performance but may also increase the load on the database.- Throws:
PersistenceException- if there is an error during the update operation, such as a violation of database constraints, connectivity issues, or if any entity in the stream is null.
-
upsert
Inserts or updates a stream of entities in the database in batches.This method processes the provided stream of entities in batches, performing an "upsert" operation on each. For each entity, it will be inserted into the database if it does not already exist; if it does exist, it will be updated to reflect the current state of the entity. Batch processing optimizes the performance of the upsert operation for larger data sets by reducing database overhead.
- Parameters:
entities- a stream of entities to be inserted or updated. Each entity in the stream must be non-null and contain valid data for insertion or update in the database.- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
upsert
Inserts or updates a stream of entities in the database in configurable batch sizes.This method processes the provided stream of entities in batches, performing an "upsert" operation on each. For each entity, it will be inserted if it does not already exist in the database, or updated if it does. The batch size can be configured to control the number of entities processed in each database operation, allowing for optimized performance and memory management based on system requirements.
- Parameters:
entities- a stream of entities to be inserted or updated. Each entity in the stream must be non-null and contain valid data for insertion or update in the database.batchSize- the number of entities to process in each batch. A larger batch size may improve performance but increase memory usage, while a smaller batch size may reduce memory usage but increase the number of database operations.- Throws:
PersistenceException- if the upsert operation fails due to database issues, such as connectivity problems, constraints violations, or invalid entity data.
-
remove
Removes a stream of entities from the database in batches.- Parameters:
entities- a stream of entities to be removed.- Throws:
PersistenceException- if the removal operation fails.
-
remove
Removes a stream of entities from the database in configurable batch sizes.- Parameters:
entities- a stream of entities to be removed.batchSize- the number of entities to process in each batch.- Throws:
PersistenceException- if the removal operation fails.
-
removeByRef
Removes a stream of entities from the database in batches.- Parameters:
refs- a stream of entities to be removed.- Throws:
PersistenceException- if the removal operation fails.
-
removeByRef
Removes a stream of entities from the database in configurable batch sizes.- Parameters:
refs- a stream of entities to be removed.batchSize- the number of entities to process in each batch.- Throws:
PersistenceException- if the removal operation fails.
-
EntityRepositorywhen preview features are enabled.