Class JdbcAggregateTemplate

java.lang.Object
org.springframework.data.jdbc.core.JdbcAggregateTemplate
All Implemented Interfaces:
Aware, ApplicationContextAware, JdbcAggregateOperations

public class JdbcAggregateTemplate extends Object implements JdbcAggregateOperations, ApplicationContextAware
JdbcAggregateOperations implementation, storing aggregates in and obtaining them from a JDBC data store.
Author:
Jens Schauder, Mark Paluch, Thomas Lang, Christoph Strobl, Milan Milanov, Myeonghyeon Lee, Chirag Tailor, Diego Krupitza, Sergey Korotaev, Mikhail Polivakha
  • Constructor Details

  • Method Details

    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Throws:
      BeansException
    • setEntityCallbacks

      public void setEntityCallbacks(EntityCallbacks entityCallbacks)
      Sets the callbacks to be invoked on life cycle events.
      Parameters:
      entityCallbacks - must not be null.
      Since:
      1.1
    • setEntityLifecycleEventsEnabled

      public void setEntityLifecycleEventsEnabled(boolean enabled)
      Configure whether lifecycle events such as AfterSaveEvent, BeforeSaveEvent, etc. should be published or whether emission should be suppressed. Enabled by default.
      Parameters:
      enabled - true to enable entity lifecycle events; false to disable entity lifecycle events.
      Since:
      3.0
      See Also:
      • AbstractRelationalEvent
    • getRowMapper

      public <T> RowMapper<? extends T> getRowMapper(Class<T> type)
      Return a RowMapper to map results for type.
      Specified by:
      getRowMapper in interface JdbcAggregateOperations
      Type Parameters:
      T - return type.
      Parameters:
      type - must not be null.
      Returns:
      a row mapper to map results for type.
      Since:
      4.0
    • save

      public <T> T save(T instance)
      Description copied from interface: JdbcAggregateOperations
      Saves an instance of an aggregate, including all the members of the aggregate.
      Specified by:
      save in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instance - the aggregate root of the aggregate to be saved. Must not be null.
      Returns:
      the saved instance.
    • saveAll

      public <T> List<T> saveAll(Iterable<T> instances)
      Description copied from interface: JdbcAggregateOperations
      Saves all aggregate instances, including all the members of each aggregate instance.
      Specified by:
      saveAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instances - the aggregate roots to be saved. Must not be null.
      Returns:
      the saved instances.
    • insert

      public <T> T insert(T instance)
      Dedicated insert function to do just the insert of an instance of an aggregate, including all the members of the aggregate.
      Specified by:
      insert in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instance - the aggregate root of the aggregate to be inserted. Must not be null.
      Returns:
      the saved instance.
    • insertAll

      public <T> List<T> insertAll(Iterable<T> instances)
      Description copied from interface: JdbcAggregateOperations
      Inserts all aggregate instances, including all the members of each aggregate instance.

      This is useful if the client provides an id for new aggregate roots.

      Specified by:
      insertAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instances - the aggregate roots to be inserted. Must not be null.
      Returns:
      the saved instances.
    • update

      public <T> T update(T instance)
      Dedicated update function to do just an update of an instance of an aggregate, including all the members of the aggregate.
      Specified by:
      update in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instance - the aggregate root of the aggregate to be inserted. Must not be null.
      Returns:
      the saved instance.
    • updateAll

      public <T> List<T> updateAll(Iterable<T> instances)
      Description copied from interface: JdbcAggregateOperations
      Updates all aggregate instances, including all the members of each aggregate instance.
      Specified by:
      updateAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instances - the aggregate roots to be inserted. Must not be null.
      Returns:
      the saved instances.
    • upsert

      public <T> T upsert(T instance)
      Description copied from interface: JdbcAggregateOperations
      Upserts an aggregate root, specifically insert if the row with the given identifier does not exist or update otherwise. The identifier must be already assigned. Only supported when the dialect supports single-statement upsert.

      Typically, this operation uses MERGE or INSERT ... ON CONFLICT syntax.

      Note: Upserts currently do not support optimistic locking.

      Specified by:
      upsert in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      instance - the aggregate root to upsert. Must not be null and must have an id set.
      Returns:
      the same instance (possibly with a generated id set if the dialect returns one).
    • count

      public long count(Class<?> domainType)
      Description copied from interface: JdbcAggregateOperations
      Counts the number of aggregates of a given type.
      Specified by:
      count in interface JdbcAggregateOperations
      Parameters:
      domainType - the type of the aggregates to be counted.
      Returns:
      the number of instances stored in the database. Guaranteed to be not null.
    • count

      public <T> long count(org.springframework.data.relational.core.query.Query query, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Counts the number of aggregates of a given type that match the given query.
      Specified by:
      count in interface JdbcAggregateOperations
      Parameters:
      query - must not be null.
      domainType - the entity type must not be null.
      Returns:
      the number of instances stored in the database. Guaranteed to be not null.
    • exists

      public <T> boolean exists(org.springframework.data.relational.core.query.Query query, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Determine whether there are aggregates that match the Query
      Specified by:
      exists in interface JdbcAggregateOperations
      Parameters:
      query - must not be null.
      domainType - the entity type must not be null.
      Returns:
      true if the object exists.
    • existsById

      public <T> boolean existsById(Object id, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Checks if an aggregate identified by type and id exists in the database.
      Specified by:
      existsById in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      id - the id of the aggregate root.
      domainType - the type of the aggregate root.
      Returns:
      whether the aggregate exists.
    • findById

      public <T> @Nullable T findById(Object id, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Load an aggregate from the database.
      Specified by:
      findById in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      id - the id of the aggregate to load. Must not be null.
      domainType - the type of the aggregate root. Must not be null.
      Returns:
      the loaded aggregate. Might return null.
    • findAll

      public <T> List<T> findAll(Class<T> domainType, Sort sort)
      Description copied from interface: JdbcAggregateOperations
      Load all aggregates of a given type, sorted.
      Specified by:
      findAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots. Must not be null.
      Parameters:
      domainType - the type of the aggregate roots. Must not be null.
      sort - the sorting information. Must not be null.
      Returns:
      Guaranteed to be not null.
    • streamAll

      public <T> Stream<T> streamAll(Class<T> domainType, Sort sort)
      Description copied from interface: JdbcAggregateOperations
      Loads all entities of the given type to a Stream, sorted.
      Specified by:
      streamAll in interface JdbcAggregateOperations
      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.
    • findAll

      @Deprecated(since="4.0", forRemoval=true) public <T> Page<T> findAll(Class<T> domainType, Pageable pageable)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Description copied from interface: JdbcAggregateOperations
      Load a page of (potentially sorted) aggregates of a given type.
      Specified by:
      findAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots. Must not be null.
      Parameters:
      domainType - the type of the aggregate roots. Must not be null.
      pageable - the pagination information. Must not be null.
      Returns:
      Guaranteed to be not null.
    • findOne

      public <T> Optional<T> findOne(org.springframework.data.relational.core.query.Query query, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Execute a SELECT query and convert the resulting item to an entity ensuring exactly one result.
      Specified by:
      findOne in interface JdbcAggregateOperations
      Parameters:
      query - must not be null.
      domainType - the entity type must not be null.
      Returns:
      exactly one result or Optional.empty() if no match found.
    • findAll

      public <T> List<T> findAll(org.springframework.data.relational.core.query.Query query, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Execute a SELECT query and convert the resulting items to a List that is sorted.
      Specified by:
      findAll in interface JdbcAggregateOperations
      Parameters:
      query - must not be null.
      domainType - the entity type must not be null.
      Returns:
      a non-null sorted list with all the matching results.
    • streamAll

      public <T> Stream<T> streamAll(org.springframework.data.relational.core.query.Query query, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Execute a SELECT query and convert the resulting items to a Stream.
      Specified by:
      streamAll in interface JdbcAggregateOperations
      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.
    • findAll

      @Deprecated(since="4.0", forRemoval=true) public <T> Page<T> findAll(org.springframework.data.relational.core.query.Query query, Class<T> domainType, Pageable pageable)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Description copied from interface: JdbcAggregateOperations
      Returns a Page of entities matching the given Query. In case no match could be found, an empty Page is returned.
      Specified by:
      findAll in interface JdbcAggregateOperations
      Parameters:
      query - must not be null.
      domainType - the entity type must not be null.
      pageable - can be null.
      Returns:
      a Page of entities matching the given Example.
    • findAll

      public <T> List<T> findAll(Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Load all aggregates of a given type.
      Specified by:
      findAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots. Must not be null.
      Parameters:
      domainType - the type of the aggregate roots. Must not be null.
      Returns:
      Guaranteed to be not null.
    • streamAll

      public <T> Stream<T> streamAll(Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Load all aggregates of a given type to a Stream.
      Specified by:
      streamAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots. Must not be null.
      Parameters:
      domainType - the type of the aggregate roots. Must not be null.
      Returns:
      Guaranteed to be not null.
    • findAllById

      public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Load all aggregates of a given type that are identified by the given ids.
      Specified by:
      findAllById in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots. Must not be null.
      Parameters:
      ids - of the aggregate roots identifying the aggregates to load. Must not be null.
      domainType - the type of the aggregate roots. Must not be null.
      Returns:
      Guaranteed to be not null.
    • streamAllByIds

      public <T> Stream<T> streamAllByIds(Iterable<?> ids, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Loads all entities that match one of the ids passed as an argument to a Stream. It is not guaranteed that the number of ids passed in matches the number of entities returned.
      Specified by:
      streamAllByIds in interface JdbcAggregateOperations
      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.
    • delete

      public <S> void delete(S aggregateRoot)
      Description copied from interface: JdbcAggregateOperations
      Delete an aggregate identified by its aggregate root.
      Specified by:
      delete in interface JdbcAggregateOperations
      Type Parameters:
      S - the type of the aggregate root.
      Parameters:
      aggregateRoot - to delete. Must not be null.
    • deleteById

      public <S> void deleteById(Object id, Class<S> domainType)
      Description copied from interface: JdbcAggregateOperations
      Deletes a single Aggregate including all entities contained in that aggregate.

      Since no version attribute is provided this method will never throw a OptimisticLockingFailureException. If no rows match the generated delete operation this fact will be silently ignored.

      Specified by:
      deleteById in interface JdbcAggregateOperations
      Type Parameters:
      S - the type of the aggregate root.
      Parameters:
      id - the id of the aggregate root of the aggregate to be deleted. Must not be null.
      domainType - the type of the aggregate root.
    • deleteAllById

      public <T> void deleteAllById(Iterable<?> ids, Class<T> domainType)
      Description copied from interface: JdbcAggregateOperations
      Deletes all aggregates identified by their aggregate root ids.

      Since no version attribute is provided this method will never throw a OptimisticLockingFailureException. If no rows match the generated delete operation this fact will be silently ignored.

      Specified by:
      deleteAllById in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate root.
      Parameters:
      ids - the ids of the aggregate roots of the aggregates to be deleted. Must not be null.
      domainType - the type of the aggregate root.
    • deleteAll

      public void deleteAll(Class<?> domainType)
      Description copied from interface: JdbcAggregateOperations
      Delete all aggregates of a given type.
      Specified by:
      deleteAll in interface JdbcAggregateOperations
      Parameters:
      domainType - type of the aggregate roots to be deleted. Must not be null.
    • deleteAll

      public <T> void deleteAll(Iterable<? extends T> instances)
      Description copied from interface: JdbcAggregateOperations
      Delete all aggregates identified by their aggregate roots.
      Specified by:
      deleteAll in interface JdbcAggregateOperations
      Type Parameters:
      T - the type of the aggregate roots.
      Parameters:
      instances - to delete. Must not be null.
    • getDataAccessStrategy

      public DataAccessStrategy getDataAccessStrategy()
      Description copied from interface: JdbcAggregateOperations
      Specified by:
      getDataAccessStrategy in interface JdbcAggregateOperations
      Returns:
      the DataAccessStrategy
    • getConverter

      public JdbcConverter getConverter()
      Description copied from interface: JdbcAggregateOperations
      Returns the JdbcConverter.
      Specified by:
      getConverter in interface JdbcAggregateOperations
      Returns:
      the JdbcConverter.