Class HibernateTopiaJpaSupport

java.lang.Object
org.nuiton.topia.persistence.internal.support.HibernateTopiaJpaSupport
All Implemented Interfaces:
TopiaJpaSupport

public class HibernateTopiaJpaSupport extends Object implements TopiaJpaSupport
This class is the Hibernate implementation of TopiaJpaSupport. It realizes the bridge between the JPA specification and the technical choice made for its implementation : Hibernate.
Since:
3.0
Author:
Arnaud Thimel (Code Lutin)
  • Field Details

    • hibernateSupport

      protected TopiaHibernateSupport hibernateSupport
      Object to get Hibernate's Session, Configuration, ...
    • firesSupport

      protected TopiaFiresSupport firesSupport
      Object that handles each event propagation. Listeners are registered inside this instance. This instance is the persistenceContext's one.
    • useFlushMode

      protected boolean useFlushMode
      This flag permits to use (or not) the flush mode when doing queries.

      The normal usage is to says yes (that's why the default value is true), in that case when doing queries (says in method findAll(String, java.util.Map) or find(String, int, int, java.util.Map)) it will use the flush mode FlushMode.AUTO).

      But sometimes, when doing a lot of queries (for some imports for example), we do NOT want the session to be flushed each time we do a find, then you can set this flag to false using the method setUseFlushMode(boolean)

      Since:
      2.5
    • slowQueriesThreshold

      protected Optional<Duration> slowQueriesThreshold
      Delay after which Topia should consider a query as slow then log a warn. If no value is present, the slow queries won't be tracked.
      Since:
      3.8
  • Constructor Details

  • Method Details

    • getHibernateSupport

      public TopiaHibernateSupport getHibernateSupport()
    • setUseFlushMode

      public void setUseFlushMode(boolean useFlushMode)
      Description copied from interface: TopiaJpaSupport
      Tells to the context if it has to use a flush mode before each query. By default, we use a flush mode, but in some case it costs to much doing this, that's why you can disable it setting the value to false.
      Specified by:
      setUseFlushMode in interface TopiaJpaSupport
      Parameters:
      useFlushMode - the new value to set
    • setSlowQueriesThreshold

      public void setSlowQueriesThreshold(Duration slowQueriesThreshold)
      Description copied from interface: TopiaJpaSupport
      Tells to the context after which delay a query should be considered as slow. Default value is to skip slow queries watching unless specified in application configuration.
      Specified by:
      setSlowQueriesThreshold in interface TopiaJpaSupport
      Parameters:
      slowQueriesThreshold - the new value to set
    • prepareQuery

      protected org.hibernate.query.Query prepareQuery(String jpaql, Map<String,Object> parameters)
    • checkHqlParameters

      protected void checkHqlParameters(Map<String,Object> parameters)
    • findAll

      public <T> List<T> findAll(String jpaql, Map<String,Object> parameters)
      Description copied from interface: TopiaJpaSupport
      Allow to do some JPA-QL query WARNING : Depending on the registered service, this method may not support something else than queries on TopiaEntity
      Specified by:
      findAll in interface TopiaJpaSupport
      Type Parameters:
      T - type of result
      Parameters:
      jpaql - the JPA-QL query
      parameters - a map which keys are the attribute names and values are the attributes expected values
      Returns:
      The result list
    • stream

      public <T> Stream<T> stream(String jpaql, Map<String,Object> parameters)
      Like findAll(String, Map) but getting a stream that may lazily fetch data. Actual behavior rely on JPA implementation. According to Query.stream(), caller should BaseStream.close() the stream.
      Specified by:
      stream in interface TopiaJpaSupport
      Since:
      3.4
    • findAny

      public <T> T findAny(String jpaql, Map<String,Object> parameters)
      Description copied from interface: TopiaJpaSupport
      Allow to do some JPA-QL query and return a single result. If nothing is found by the query, will return null. WARNING : Depending on the registered service, this method may not support something else than queries on TopiaEntity
      Specified by:
      findAny in interface TopiaJpaSupport
      Type Parameters:
      T - type of result
      Parameters:
      jpaql - the JPA-QL query
      parameters - a map which keys are the attribute names and values are the attributes expected values
      Returns:
      The result instance or null
    • findUnique

      public <T> T findUnique(String jpaql, Map<String,Object> parameters)
      Description copied from interface: TopiaJpaSupport
      Allow to do some JPA-QL query and return an unique result. If nothing is found by the query, will return null. If more than one result is found, will throw an exception. WARNING : Depending on the registered service, this method may not support something else than queries on TopiaEntity
      Specified by:
      findUnique in interface TopiaJpaSupport
      Type Parameters:
      T - type of result
      Parameters:
      jpaql - the JPA-QL query
      parameters - a map which keys are the attribute names and values are the attributes expected values
      Returns:
      The result instance or null
    • hqlContainsOrderBy

      protected boolean hqlContainsOrderBy(String hql)
    • find

      public <T> List<T> find(String jpaql, int startIndex, int endIndex, Map<String,Object> parameters) throws QueryMissingOrderException
      Description copied from interface: TopiaJpaSupport
      Allow to do some JPA-QL query using the given bounds.
      • No lower bound : startIndex = 0.
      • No upper bound : endIndex = -1.
      WARNING : Depending on the registered service, this method may not support something else than queries on TopiaEntity
      Specified by:
      find in interface TopiaJpaSupport
      Type Parameters:
      T - type of result
      Parameters:
      jpaql - the JPA-QL query
      startIndex - first index of entity to return
      endIndex - last index of entity to return
      parameters - a map which keys are the attribute names and values are the attributes expected values
      Returns:
      The result list
      Throws:
      QueryMissingOrderException - if no order by clause is specified
    • find0

      protected <T> List<T> find0(String jpaql, int startIndex, int endIndex, Map<String,Object> parameters)
    • execute

      public int execute(String jpaql, Map<String,Object> parameters)
      Description copied from interface: TopiaJpaSupport
      Execute JPA-QL operation on data (Update, Delete).
      Specified by:
      execute in interface TopiaJpaSupport
      Parameters:
      jpaql - the JPA-QL query
      parameters - a map which keys are the attribute names and values are the attributes expected values
      Returns:
      The number of entities updated or deleted.
    • save

      public void save(Object object)
      Description copied from interface: TopiaJpaSupport
      Persist the given transient instance, first assigning a generated identifier. This method is JPA implementation independent. This method is "inspired" of the Hibernate's Session#save method.
      Specified by:
      save in interface TopiaJpaSupport
      Parameters:
      object - a transient instance of a persistent class
    • update

      public void update(Object object)
      Description copied from interface: TopiaJpaSupport
      Update the persistent instance with the identifier of the given detached instance. This method is "inspired" of the Hibernate's Session#update method.
      Specified by:
      update in interface TopiaJpaSupport
      Parameters:
      object - a detached instance containing updated state
    • saveOrUpdate

      public void saveOrUpdate(Object object)
      Description copied from interface: TopiaJpaSupport
      Either TopiaJpaSupport.save(Object) or TopiaJpaSupport.update(Object) the given instance. This method is "inspired" of the Hibernate's Session#saveOrUpdate method.
      Specified by:
      saveOrUpdate in interface TopiaJpaSupport
      Parameters:
      object - a transient or detached instance containing new or updated state
      See Also:
    • delete

      public void delete(Object object)
      Description copied from interface: TopiaJpaSupport
      Remove a persistent instance. This method is "inspired" of the Hibernate's Session#delete method.
      Specified by:
      delete in interface TopiaJpaSupport
      Parameters:
      object - the instance to be removed
    • monitorQuery

      protected SlowQueryWatcher monitorQuery(org.hibernate.query.Query query, Map<String,Object> args)
    • truncateLongArgs

      protected Map<String,Object> truncateLongArgs(Map<String,Object> args, int truncateSize)
      Some of the values may be very long. This methods truncates any Iterable value to a maximum of truncateSize elements and display how many more.
      Parameters:
      args - the raw map of args
      truncateSize - who many elements to include before truncating
      Returns:
      a view over the given map which each value is potentially truncated