Class HibernateUtils
java.lang.Object
org.broadleafcommerce.common.util.HibernateUtils
Hibernate convenience methods
- Author:
- Philip Baggett (pbaggett)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCalls theEntityManager.clear()method on the current default (blPU)EntityManagerif it is bound to a thread via theTransactionSynchronizationManager.static voidclearEntityManager(String persistenceUnitName) Calls theEntityManager.clear()method on the currentEntityManagerwith the provide persistence unit name if it is bound to a thread via theTransactionSynchronizationManager.static <T> Tdeproxy(T t) Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation.static <T> TexecuteWithCache(GenericOperation<T> operation) Attempts to execute the operation with L2 and query cache engaged.static <T> TexecuteWithCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em) During bulk operations such as reindexing we occasionally want things to be cached.static <T> TexecuteWithCache(GenericOperation<T> operation, String persistenceUnitName) Attempts to execute the operation with L2 and query cache engaged.static <T> TexecuteWithoutCache(GenericOperation<T> operation) Attempts to execute the operation without L2 or query cache engaged.static <T> TexecuteWithoutCache(GenericOperation<T> operation, jakarta.persistence.EntityManager em) During bulk operations such as reindexing we don't always want things to be cached.static <T> TexecuteWithoutCache(GenericOperation<T> operation, String persistenceUnitName) Attempts to execute the operation without L2 or query cache engaged.static jakarta.persistence.EntityManagerRetrieves the current, defaultEntityManager, with thePersistenceUnitname "blPU", or null if it has not been initialized and bound to the Thread.static jakarta.persistence.EntityManagergetCurrentEntityManager(String persistenceUnitName) Retrieves the current, defaultEntityManager, with thePersistenceUnitname provided, or null if it has not been initialized and bound to the Thread.
-
Field Details
-
DEFAULT_ENTITY_MANAGER_NAME
- See Also:
-
-
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 theEntityManageris null, the operation is executed without affecting the cache settings.- Parameters:
operation-em-- Returns:
- Throws:
GException
-
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 thePersistenceUnitwith the provided name. If noEntityManageris bound to the thread for the providedPersistenceUnit, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManagerif one is not already initialized.- Parameters:
operation-persistenceUnitName-- Returns:
- Throws:
Exception
-
executeWithCache
Attempts to execute the operation with L2 and query cache engaged. This method uses thePersistenceUnitname "blPU". If noEntityManageris bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManagerif 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 theEntityManageris 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 thePersistenceUnitwith the provided name. If noEntityManageris bound to the thread for the providedPersistenceUnit, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManagerif one is not already initialized.- Parameters:
operation-persistenceUnitName-- Returns:
- Throws:
Exception
-
executeWithoutCache
Attempts to execute the operation without L2 or query cache engaged. This method uses thePersistenceUnitname "blPU". If noEntityManageris bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManagerif one is not already initialized.- Parameters:
operation-- Returns:
- Throws:
Exception
-
getCurrentEntityManager
Retrieves the current, defaultEntityManager, with thePersistenceUnitname 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, defaultEntityManager, with thePersistenceUnitname "blPU", or null if it has not been initialized and bound to the Thread.- Returns:
-
clearDefaultEntityManager
public static void clearDefaultEntityManager()Calls theEntityManager.clear()method on the current default (blPU)EntityManagerif it is bound to a thread via theTransactionSynchronizationManager. If not, it is ignored. -
clearEntityManager
Calls theEntityManager.clear()method on the currentEntityManagerwith the provide persistence unit name if it is bound to a thread via theTransactionSynchronizationManager. If not, it is ignored.
-