Class HibernateUtils

java.lang.Object
org.broadleafcommerce.common.util.HibernateUtils

public class HibernateUtils extends Object
Hibernate convenience methods
Author:
Philip Baggett (pbaggett)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Calls the EntityManager.clear() method on the current default (blPU) EntityManager if it is bound to a thread via the TransactionSynchronizationManager.
    static void
    clearEntityManager(String persistenceUnitName)
    Calls the EntityManager.clear() method on the current EntityManager with the provide persistence unit name if it is bound to a thread via the TransactionSynchronizationManager.
    static <T> T
    deproxy(T t)
    Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation.
    static <T> T
    Attempts to execute the operation with L2 and query cache engaged.
    static <T> T
    executeWithCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em)
    During bulk operations such as reindexing we occasionally want things to be cached.
    static <T> T
    executeWithCache(GenericOperation<T> operation, String persistenceUnitName)
    Attempts to execute the operation with L2 and query cache engaged.
    static <T> T
    Attempts to execute the operation without L2 or query cache engaged.
    static <T> T
    executeWithoutCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em)
    During bulk operations such as reindexing we don't always want things to be cached.
    static <T> T
    executeWithoutCache(GenericOperation<T> operation, String persistenceUnitName)
    Attempts to execute the operation without L2 or query cache engaged.
    static jakarta.persistence.EntityManager
    Retrieves the current, default EntityManager, with the PersistenceUnit name "blPU", or null if it has not been initialized and bound to the Thread.
    static jakarta.persistence.EntityManager
    getCurrentEntityManager(String persistenceUnitName)
    Retrieves the current, default EntityManager, with the PersistenceUnit name provided, or null if it has not been initialized and bound to the Thread.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • HibernateUtils

      public HibernateUtils()
  • Method Details

    • deproxy

      public static <T> T deproxy(T t)

      Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation.

      This is primarily useful when retrieving a lazy loaded object that has been subclassed and you have the intention of casting it.

      Parameters:
      t - the domain object to deproxy
      Returns:
      the actual persisted object or the passed in object if it is not a Hibernate proxy
    • executeWithCache

      public static <T> T executeWithCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em) throws Exception
      During bulk operations such as reindexing we occasionally want things to be cached. This allows us to surgically turn on caching where appropriate. If the EntityManager is null, the operation is executed without affecting the cache settings.
      Parameters:
      operation -
      em -
      Returns:
      Throws:
      G
      Exception
    • executeWithCache

      public static <T> T executeWithCache(GenericOperation<T> operation, String persistenceUnitName) throws Exception
      Attempts to execute the operation with L2 and query cache engaged. This affects the PersistenceUnit with the provided name. If no EntityManager is bound to the thread for the provided PersistenceUnit, then the operation is performed without modifying the cache mode. Note that this does not create an EntityManager if one is not already initialized.
      Parameters:
      operation -
      persistenceUnitName -
      Returns:
      Throws:
      Exception
    • executeWithCache

      public static <T> T executeWithCache(GenericOperation<T> operation) throws Exception
      Attempts to execute the operation with L2 and query cache engaged. This method uses the PersistenceUnit name "blPU". If no EntityManager is bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create an EntityManager if one is not already initialized.
      Parameters:
      operation -
      Returns:
      Throws:
      Exception
    • executeWithoutCache

      public static <T> T executeWithoutCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em) throws Exception
      During bulk operations such as reindexing we don't always want things to be cached. This allows us to surgically turn off caching where appropriate. If the EntityManager is null, the operation is executed without affecting the cache settings.
      Parameters:
      operation -
      em -
      Returns:
      Throws:
      Exception
    • executeWithoutCache

      public static <T> T executeWithoutCache(GenericOperation<T> operation, String persistenceUnitName) throws Exception
      Attempts to execute the operation without L2 or query cache engaged. This affects the PersistenceUnit with the provided name. If no EntityManager is bound to the thread for the provided PersistenceUnit, then the operation is performed without modifying the cache mode. Note that this does not create an EntityManager if one is not already initialized.
      Parameters:
      operation -
      persistenceUnitName -
      Returns:
      Throws:
      Exception
    • executeWithoutCache

      public static <T> T executeWithoutCache(GenericOperation<T> operation) throws Exception
      Attempts to execute the operation without L2 or query cache engaged. This method uses the PersistenceUnit name "blPU". If no EntityManager is bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create an EntityManager if one is not already initialized.
      Parameters:
      operation -
      Returns:
      Throws:
      Exception
    • getCurrentEntityManager

      public static jakarta.persistence.EntityManager getCurrentEntityManager(String persistenceUnitName)
      Retrieves the current, default EntityManager, with the PersistenceUnit name provided, or null if it has not been initialized and bound to the Thread.
      Parameters:
      persistenceUnitName -
      Returns:
    • getCurrentDefaultEntityManager

      public static jakarta.persistence.EntityManager getCurrentDefaultEntityManager()
      Retrieves the current, default EntityManager, with the PersistenceUnit name "blPU", or null if it has not been initialized and bound to the Thread.
      Returns:
    • clearDefaultEntityManager

      public static void clearDefaultEntityManager()
      Calls the EntityManager.clear() method on the current default (blPU) EntityManager if it is bound to a thread via the TransactionSynchronizationManager. If not, it is ignored.
    • clearEntityManager

      public static void clearEntityManager(String persistenceUnitName)
      Calls the EntityManager.clear() method on the current EntityManager with the provide persistence unit name if it is bound to a thread via the TransactionSynchronizationManager. If not, it is ignored.