Interface DataAccessStrategy

All Superinterfaces:
RelationResolver
All Known Implementing Classes:
CascadingDataAccessStrategy, DefaultDataAccessStrategy, DelegatingDataAccessStrategy, MyBatisDataAccessStrategy

public interface DataAccessStrategy extends RelationResolver
Abstraction for accesses to the database that should be implementable with a single SQL statement per method and relates to a single entity as opposed to JdbcAggregateOperations which provides interactions related to complete aggregates.
Author:
Jens Schauder, Tyler Van Gorder, Milan Milanov, Myeonghyeon Lee, Chirag Tailor, Diego Krupitza
  • Method Details

    • insert

      @Nullable <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource)
      Inserts the data of a single entity. Referenced entities don't get handled.
      Type Parameters:
      T - the type of the instance.
      Parameters:
      instance - the instance to be stored. Must not be null.
      domainType - the type of the instance. Must not be null.
      identifier - information about data that needs to be considered for the insert but which is not part of the entity. Namely, references back to a parent entity and key/index columns for entities that are stored in a Map or List.
      idValueSource - the IdValueSource for the insert.
      Returns:
      the id generated by the database if any.
      Since:
      2.4
    • insert

      <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource)
      Inserts the data of multiple entities.
      Type Parameters:
      T - the type of the instance.
      Parameters:
      insertSubjects - the subjects to be inserted, where each subject contains the instance and its identifier. Must not be null.
      domainType - the type of the instance. Must not be null.
      idValueSource - the IdValueSource for the insert.
      Returns:
      the ids corresponding to each record that was inserted, if ids were generated. If ids were not generated, elements will be null.
      Since:
      2.4
    • update

      <T> boolean update(T instance, Class<T> domainType)
      Updates the data of a single entity in the database. Referenced entities don't get handled.
      Type Parameters:
      T - the type of the instance to save.
      Parameters:
      instance - the instance to save. Must not be null.
      domainType - the type of the instance to save. Must not be null.
      Returns:
      whether the update actually updated a row.
    • updateWithVersion

      <T> boolean updateWithVersion(T instance, Class<T> domainType, Number previousVersion)
      Updates the data of a single entity in the database and enforce optimistic record locking using the previousVersion property. Referenced entities don't get handled.

      The statement will be of the form : UPDATE … SET … WHERE ID = :id and VERSION_COLUMN = :previousVersion and throw an optimistic record locking exception if no rows have been updated.

      Type Parameters:
      T - the type of the instance to save.
      Parameters:
      instance - the instance to save. Must not be null.
      domainType - the type of the instance to save. Must not be null.
      previousVersion - The previous version assigned to the instance being saved.
      Returns:
      whether the update actually updated a row.
      Throws:
      OptimisticLockingFailureException - if the update fails to update at least one row assuming the optimistic locking version check failed.
      Since:
      2.0
    • delete

      void delete(Object id, Class<?> domainType)
      Deletes a single row identified by the id, from the table identified by the domainType. Does not handle cascading deletes.

      The statement will be of the form : DELETE FROM … WHERE ID = :id and VERSION_COLUMN = :version and throw an optimistic record locking exception if no rows have been updated.

      Parameters:
      id - the id of the row to be deleted. Must not be null.
      domainType - the type of entity to be deleted. Implicitly determines the table to operate on. Must not be null.
    • delete

      void delete(Iterable<Object> ids, Class<?> domainType)
      Deletes multiple rows identified by the ids, from the table identified by the domainType. Does not handle cascading deletes.

      The statement will be of the form : DELETE FROM … WHERE ID IN (:ids) and throw an optimistic record locking exception if no rows have been updated.

      Parameters:
      ids - the ids of the rows to be deleted. Must not be null.
      domainType - the type of entity to be deleted. Implicitly determines the table to operate on. Must not be null.
      Since:
      3.0
    • deleteWithVersion

      <T> void deleteWithVersion(Object id, Class<T> domainType, Number previousVersion)
      Deletes a single entity from the database and enforce optimistic record locking using the version property. Does not handle cascading deletes.
      Parameters:
      id - the id of the row to be deleted. Must not be null.
      domainType - the type of entity to be deleted. Implicitly determines the table to operate on. Must not be null.
      previousVersion - The previous version assigned to the instance being saved.
      Throws:
      OptimisticLockingFailureException - if the update fails to update at least one row assuming the optimistic locking version check failed.
      Since:
      2.0
    • delete

      void delete(Object rootId, PersistentPropertyPath<RelationalPersistentProperty> propertyPath)
      Deletes all entities reachable via propertyPath from the instance identified by rootId.
      Parameters:
      rootId - Id of the root object on which the propertyPath is based. Must not be null.
      propertyPath - Leading from the root object to the entities to be deleted. Must not be null.
    • delete

      void delete(Iterable<Object> rootIds, PersistentPropertyPath<RelationalPersistentProperty> propertyPath)
      Deletes all entities reachable via propertyPath from the instances identified by rootIds.
      Parameters:
      rootIds - Ids of the root objects on which the propertyPath is based. Must not be null or empty.
      propertyPath - Leading from the root object to the entities to be deleted. Must not be null.
    • deleteAll

      <T> void deleteAll(Class<T> domainType)
      Deletes all entities of the given domain type.
      Type Parameters:
      T - type of the domain type.
      Parameters:
      domainType - the domain type for which to delete all entries. Must not be null.
    • deleteAll

      Deletes all entities reachable via propertyPath from any instance.
      Parameters:
      propertyPath - Leading from the root object to the entities to be deleted. Must not be null.
    • acquireLockById

      <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType)
      Acquire a lock on the aggregate specified by id.
      Parameters:
      id - the id of the entity to load. Must not be null.
      lockMode - the lock mode for select. Must not be null.
      domainType - the domain type of the entity. Must not be null.
    • acquireLockAll

      <T> void acquireLockAll(LockMode lockMode, Class<T> domainType)
      Acquire a lock on all aggregates of the given domain type.
      Parameters:
      lockMode - the lock mode for select. Must not be null.
      domainType - the domain type of the entity. Must not be null.
    • count

      long count(Class<?> domainType)
      Counts the rows in the table representing the given domain type.
      Parameters:
      domainType - the domain type for which to count the elements. Must not be null.
      Returns:
      the count. Guaranteed to be not null.
    • count

      <T> long count(Query query, Class<T> domainType)
      Counts the rows in the table representing the given probe type, that match the given query.
      Parameters:
      domainType - the probe type for which to count the elements. Must not be null.
      query - the query which elements have to match.
      Returns:
      the count. Guaranteed to be not null.
      Since:
      3.0
    • exists

      <T> boolean exists(Query query, Class<T> domainType)
      Determine whether there is an aggregate of type domainType that matches the provided Query.
      Parameters:
      query - must not be null.
      domainType - the type of entities. Must not be null.
      Returns:
      true if the object exists.
      Since:
      3.0
    • existsById

      <T> boolean existsById(Object id, Class<T> domainType)
      returns if a row with the given id exists for the given type.
      Type Parameters:
      T - the type of the entity.
      Parameters:
      id - the id of the entity for which to check. Must not be null.
      domainType - the type of the entity to check for. Must not be null.
      Returns:
      true if a matching row exists, otherwise false.
    • findById

      @Nullable <T> T findById(Object id, Class<T> domainType)
      Loads a single entity identified by type and id.
      Type Parameters:
      T - the type of the entity.
      Parameters:
      id - the id of the entity to load. Must not be null.
      domainType - the domain type of the entity. Must not be null.
      Returns:
      Might return null.
    • findAll

      <T> Iterable<T> findAll(Class<T> domainType)
      Loads all entities of the given type.
      Type Parameters:
      T - the type of entities to load.
      Parameters:
      domainType - the type of entities to load. Must not be null.
      Returns:
      Guaranteed to be not null.
    • findAllById

      <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType)
      Loads all entities that match one of the ids passed as an argument. It is not guaranteed that the number of ids passed in matches the number of entities returned.
      Type Parameters:
      T - type of entities to load.
      Parameters:
      ids - the Ids of the entities to load. Must not be null.
      domainType - the type of entities to load. Must not be null.
      Returns:
      the loaded entities. Guaranteed to be not null.
    • findAllByPath

      Iterable<Object> findAllByPath(Identifier identifier, PersistentPropertyPath<? extends RelationalPersistentProperty> path)
      Description copied from interface: RelationResolver
      Finds all entities reachable via path.
      Specified by:
      findAllByPath in interface RelationResolver
      Parameters:
      identifier - the combination of Id, map keys and list indexes that identify the parent of the entity to be loaded. Must not be null.
      path - the path from the aggregate root to the entities to be resolved. Must not be null.
      Returns:
      guaranteed to be not null.
    • findAll

      <T> Iterable<T> findAll(Class<T> domainType, Sort sort)
      Loads all entities of the given type, sorted.
      Type Parameters:
      T - the type of entities to load.
      Parameters:
      domainType - the type of entities to load. Must not be null.
      sort - the sorting information. Must not be null.
      Returns:
      Guaranteed to be not null.
      Since:
      2.0
    • findAll

      <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable)
      Loads all entities of the given type, paged and sorted.
      Type Parameters:
      T - the type of entities to load.
      Parameters:
      domainType - the type of entities to load. Must not be null.
      pageable - the pagination information. Must not be null.
      Returns:
      Guaranteed to be not null.
      Since:
      2.0
    • findOne

      <T> Optional<T> findOne(Query query, Class<T> domainType)
      Execute a SELECT query and convert the resulting item to an entity ensuring exactly one result.
      Parameters:
      query - must not be null.
      domainType - the type of entities. Must not be null.
      Returns:
      exactly one result or Optional.empty() if no match found.
      Throws:
      IncorrectResultSizeDataAccessException - if more than one match found.
      Since:
      3.0
    • findAll

      <T> Iterable<T> findAll(Query query, Class<T> domainType)
      Execute a SELECT query and convert the resulting items to a Iterable.
      Parameters:
      query - must not be null.
      domainType - the type of entities. Must not be null.
      Returns:
      a non-null list with all the matching results.
      Throws:
      IncorrectResultSizeDataAccessException - if more than one match found.
      Since:
      3.0
    • findAll

      <T> Iterable<T> findAll(Query query, Class<T> domainType, Pageable pageable)
      Execute a SELECT query and convert the resulting items to a Iterable. Applies the Pageable to the result.
      Parameters:
      query - must not be null.
      domainType - the type of entities. Must not be null.
      pageable - the pagination that should be applied. Must not be null.
      Returns:
      a non-null list with all the matching results.
      Throws:
      IncorrectResultSizeDataAccessException - if more than one match found.
      Since:
      3.0