Interface PersistenceService

All Known Implementing Classes:
PersistenceServiceImpl

public interface PersistenceService
This is the main service to be used by other modules for database interactions.
  • Method Details

    • returnFromTransaction

      <T> T returnFromTransaction(CheckedFunction<TransactionControl,T> function)
      Run the given function function inside a transaction. If a transaction is already running, the existing transaction is used. Newly created entities will automatically be inserted before committing.
      Type Parameters:
      T - the return type
      Parameters:
      function - the function to run
      Returns:
      the result of the function
    • doInTransaction

      void doInTransaction(CheckedConsumer<TransactionControl> consumer)
      The same as returnFromTransaction, but accepts a Consumer instead of a Function and has therefore no return type.
      Parameters:
      consumer - the function to run
    • returnFromNewSession

      <T> T returnFromNewSession(Supplier<T> supplier)
      Run the given supplier with a new session
      Type Parameters:
      T - the return type
      Parameters:
      supplier - the supplier to run
      Returns:
      the result of the supplier
    • doInNewSession

      void doInNewSession(Runnable runnable)
      Same as returnFromNewSession but without a return type
      Parameters:
      runnable - to run in a new session
    • create

      <T> T create(Class<T> interfaceClass)
      Returns:
      an instance of the given entity interface
    • create

      Entity create(String modelName)
      Returns:
      an instance of the given entity model
    • get

      <T extends Entity> T get(Class<T> entityClass, Serializable pk)
      Returns:
      the entity with the given identifier
      Throws:
      NotFoundException - if no such entity exists
    • get

      <T extends Entity> T get(Class<T> entityClass, String identifier)
      Parameters:
      identifier - the unique_id or string key of an entity
      Returns:
      the entity with the given identifier
      Throws:
      NotFoundException - if no such entity exists
    • get

      Entity get(String modelName, Serializable id)
      Returns:
      the entity with the given identifier
      Throws:
      NotFoundException - if no such entity exists
    • get

      Entity get(String modelName, String identifier)
      Parameters:
      identifier - the unique_id or string key of an entity
      Returns:
      the entity with the given identifier
      Throws:
      NotFoundException - if no such entity exists
    • getIfPresent

      <T extends Entity> Optional<T> getIfPresent(Class<T> entityClass, Serializable pk)
      Returns:
      the entity with the given identifier or an empty optional if it does not exist
    • getIfPresent

      <T extends Entity> Optional<T> getIfPresent(Class<T> entityClass, String identifier)
      Parameters:
      identifier - the unique_id or string key of an entity
      Returns:
      the entity with the given identifier or an empty optional if it does not exist
    • getIfPresent

      Optional<Entity> getIfPresent(String modelName, Serializable id)
      Returns:
      the entity with the given identifier or an empty optional if it does not exist
    • getIfPresent

      Optional<Entity> getIfPresent(String modelName, String identifier)
      Parameters:
      identifier - the unique_id or string key of an entity
      Returns:
      the entity with the given identifier or an empty optional if it does not exist
    • queryAll

      <T extends Entity> List<T> queryAll(Class<T> entityClass)
      Returns:
      all entities of the given class
    • queryAll

      List<Entity> queryAll(String modelName)
      Returns:
      all entities of the given class
    • beginTransaction

      TransactionControl beginTransaction(boolean rollbackOnly)
      Start a new transaction. Preferably doInTransaction(CheckedConsumer) and returnFromTransaction(CheckedFunction) should be used. The transaction handling (commit/rollback) must be handled by the user of this method.
      Parameters:
      rollbackOnly - if the transaction should be rolled back at the end of the transaction
      Returns:
      the TransactionControl for the newly created transaction
      See Also:
    • beginTransaction

      default TransactionControl beginTransaction()
      See Also:
    • getCurrentTransaction

      @Nullable @Nullable TransactionControl getCurrentTransaction()
      Returns:
      a TransactionControl for the currently running transaction (or null)
    • getCurrentSession

      PersistenceSession getCurrentSession()
      Returns:
      the currently active session - this may open a new thread-bound session.
    • doWithConnection

      <T> T doWithConnection(org.hibernate.jdbc.ReturningWork<T> work)
      Execute an instance of ReturningWork using the connection (transaction) of the current session.
    • txInvoker

      Invoker txInvoker()
    • createDeleteBuilder

      CriteriaDeleteBuilder createDeleteBuilder(Class<? extends Entity> entityClass)
      Create a new delete builder for the current Session.
      Parameters:
      entityClass - the root entity (which is also the return type)
    • createDeleteBuilder

      CriteriaDeleteBuilder createDeleteBuilder(String modelName)
      Create a new delete builder for the current Session.
      Parameters:
      modelName - the model name of the root entity
    • compileQuery

    • createQueryBuilder

      <T extends Entity> EntityQueryBuilder<T> createQueryBuilder(Class<T> entityClass)
      Create a new EntityQueryBuilder that returns Entity instances of the given type.
      Parameters:
      entityClass - the root entity (which is also the return type)
    • createQueryBuilder

      EntityQueryBuilder<Entity> createQueryBuilder(String modelName)
      Create a new EntityQueryBuilder that returns Entity instances of the given type.
      Parameters:
      modelName - the model name of the root entity
    • createQueryBuilder

      <T extends Entity> EntityQueryBuilder<T> createQueryBuilder(Class<T> entityClass, Map<String,?> hints)
      Create a new EntityQueryBuilder that returns Entity instances of the given type.
      Parameters:
      entityClass - the root entity (which is also the return type)
      hints - optional hints about the query (see QueryHints for available hints)
    • createQueryBuilder

      EntityQueryBuilder<Entity> createQueryBuilder(String modelName, Map<String,?> hints)
      Create a new EntityQueryBuilder that returns Entity instances of the given type.
      Parameters:
      modelName - the model name of the root entity
      hints - optional hints about the query (see QueryHints for available hints)
    • createQueryBuilder

      <T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType)
      Create a new SinglePathQueryBuilder that returns a single path of the root entity.
      Parameters:
      entityClass - the root entity
      resultType - the type of the path to be returned
    • createQueryBuilder

      <T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType)
      Create a new SinglePathQueryBuilder that returns a single path of the root entity.
      Parameters:
      modelName - the model name of the root entity
      resultType - the type of the path to be returned
    • createQueryBuilder

      <T> SinglePathQueryBuilder<T> createQueryBuilder(Class<? extends Entity> entityClass, Class<T> resultType, Map<String,?> hints)
      Create a new SinglePathQueryBuilder that returns a single path of the root entity.
      Parameters:
      entityClass - the root entity
      resultType - the type of the path to be returned
      hints - optional hints about the query (see QueryHints for available hints)
    • createQueryBuilder

      <T> SinglePathQueryBuilder<T> createQueryBuilder(String modelName, Class<T> resultType, Map<String,?> hints)
      Create a new SinglePathQueryBuilder that returns a single path of the root entity.
      Parameters:
      modelName - the model name of the root entity
      resultType - the type of the path to be returned
      hints - optional hints about the query (see QueryHints for available hints)
    • createQueryBuilderForPaths

      <T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass)
      Create a new PathQueryBuilder that returns an Object[] containing multiple paths of the root entity.
      Parameters:
      entityClass - the root entity
      targetClass - the return type of the query builder (for example Map or Object[]
    • createQueryBuilderForPaths

      <T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass)
      Create a new PathQueryBuilder that returns an Object[] containing multiple paths of the root entity.
      Parameters:
      modelName - the model name of the root entity
      targetClass - the return type of the query builder (for example Map or Object[]
    • createQueryBuilderForPaths

      <T> PathQueryBuilder<T> createQueryBuilderForPaths(Class<? extends Entity> entityClass, Class<T> targetClass, Map<String,?> hints)
      Create a new PathQueryBuilder that returns an Object[] containing multiple paths of the root entity.
      Parameters:
      entityClass - the root entity
      targetClass - the return type of the query builder (for example Map or Object[]
      hints - optional hints about the query (see QueryHints for available hints)
    • createQueryBuilderForPaths

      <T> PathQueryBuilder<T> createQueryBuilderForPaths(String modelName, Class<T> targetClass, Map<String,?> hints)
      Create a new PathQueryBuilder that returns an Object[] containing multiple paths of the root entity.
      Parameters:
      modelName - the model name of the root entity
      targetClass - the return type of the query builder (for example Map or Object[]
      hints - optional hints about the query (see QueryHints for available hints)
    • createCountQueryBuilder

      CountQueryBuilder createCountQueryBuilder(Class<? extends Entity> entityClass)
      Create a new CountQueryBuilder which executes count queries.
      Parameters:
      entityClass - the root entity
    • createCountQueryBuilder

      CountQueryBuilder createCountQueryBuilder(String modelName)
      Create a new CountQueryBuilder which executes count queries.
      Parameters:
      modelName - the model name of the root entity
    • createCountQueryBuilder

      CountQueryBuilder createCountQueryBuilder(Class<? extends Entity> entityClass, Map<String,?> hints)
      Create a new CountQueryBuilder which executes count queries.
      Parameters:
      entityClass - the root entity
      hints - optional hints about the query (see QueryHints for available hints)
    • createCountQueryBuilder

      CountQueryBuilder createCountQueryBuilder(String modelName, Map<String,?> hints)
      Create a new CountQueryBuilder which executes count queries.
      Parameters:
      modelName - the model name of the root entity
      hints - optional hints about the query (see QueryHints for available hints)
    • createPrimaryKeyQueryBuilder

      PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass)
      Create a new PrimaryKeyQueryBuilder which executes queries for PrimaryKey instances.
      Parameters:
      entityClass - the root entity
    • createPrimaryKeyQueryBuilder

      PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(String modelName)
      Create a new PrimaryKeyQueryBuilder which executes queries for PrimaryKey instances.
      Parameters:
      modelName - the model name of the root entity
    • createPrimaryKeyQueryBuilder

      PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(Class<? extends Entity> entityClass, Map<String,?> hints)
      Create a new PrimaryKeyQueryBuilder which executes queries for PrimaryKey instances.
      Parameters:
      entityClass - the root entity
      hints - optional hints about the query (see QueryHints for available hints)
    • createPrimaryKeyQueryBuilder

      PrimaryKeyQueryBuilder createPrimaryKeyQueryBuilder(String modelName, Map<String,?> hints)
      Create a new PrimaryKeyQueryBuilder which executes queries for PrimaryKey instances.
      Parameters:
      modelName - the model name of the root entity
      hints - optional hints about the query (see QueryHints for available hints)
    • loadEntityClassByName

      <T extends Entity> Class<T> loadEntityClassByName(String entityName)
      Utility method to load the dynamically generated entity class by entity name.
      Parameters:
      entityName - name of the entity to load. Can be the fully qualified class name or just the entity name
      Returns:
      the entity class instance
    • addSessionListener

      void addSessionListener(SessionFactoryManagerListener listener)
    • removeSessionListener

      void removeSessionListener(SessionFactoryManagerListener listener)