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 void
Calls theEntityManager.clear()
method on the current default (blPU)EntityManager
if it is bound to a thread via theTransactionSynchronizationManager
.static void
clearEntityManager
(String persistenceUnitName) Calls theEntityManager.clear()
method on the currentEntityManager
with the provide persistence unit name if it is bound to a thread via theTransactionSynchronizationManager
.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
executeWithCache
(GenericOperation<T> operation) 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
executeWithoutCache
(GenericOperation<T> operation) 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, defaultEntityManager
, with thePersistenceUnit
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, defaultEntityManager
, with thePersistenceUnit
name 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 theEntityManager
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 thePersistenceUnit
with the provided name. If noEntityManager
is bound to the thread for the providedPersistenceUnit
, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManager
if 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 thePersistenceUnit
name "blPU". If noEntityManager
is bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManager
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 theEntityManager
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 thePersistenceUnit
with the provided name. If noEntityManager
is bound to the thread for the providedPersistenceUnit
, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManager
if 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 thePersistenceUnit
name "blPU". If noEntityManager
is bound to the thread, then the operation is performed without modifying the cache mode. Note that this does not create anEntityManager
if one is not already initialized.- Parameters:
operation
-- Returns:
- Throws:
Exception
-
getCurrentEntityManager
Retrieves the current, defaultEntityManager
, with thePersistenceUnit
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, defaultEntityManager
, with thePersistenceUnit
name "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)EntityManager
if it is bound to a thread via theTransactionSynchronizationManager
. If not, it is ignored. -
clearEntityManager
Calls theEntityManager.clear()
method on the currentEntityManager
with the provide persistence unit name if it is bound to a thread via theTransactionSynchronizationManager
. If not, it is ignored.
-