Class ThawTestUtils

java.lang.Object
ubic.gemma.core.util.test.ThawTestUtils

public final class ThawTestUtils extends Object
Helpers for tests that need to assert lazy-association initialization state before and after a service's thaw(...) call.

The default test transactional session keeps the entity managed with its bag often pre-hydrated from cascade-saves earlier in the same test method. That makes "is bag NOT initialized" assertions fail before the thaw() even runs. BaseDatabaseTest5 disables L2 cache, so the staleness lives entirely in the first-level cache.

Opening a fresh Session sidesteps that first-level cache and gives a clean view of the entity, with all lazy associations uninitialized. The session is closed before returning, so the result is detached — fine for downstream service.thaw(...) calls that re-attach via ensureInSession() inside their own read-only transaction, and fine for Hibernate.isInitialized(Object) which is safe on detached proxies.

Author:
Gemma
  • Method Details

    • loadDetachedInFreshSession

      public static <T> T loadDetachedInFreshSession(SessionFactory sessionFactory, Class<T> type, Long id)
      Load an entity in a fresh Session, then close that session, returning a detached instance whose lazy associations are unrealised.
      Type Parameters:
      T - entity type
      Parameters:
      sessionFactory - the test session factory
      type - entity class
      id - primary key
      Returns:
      the detached instance with all lazy associations uninitialized
    • queryDetachedInFreshSession

      public static <T> T queryDetachedInFreshSession(SessionFactory sessionFactory, Function<Session,T> query)
      Run a query inside a fresh Session and return its result detached. The query function executes against a brand-new session (independent of the thread-bound transactional session) so any entities it loads come back with their lazy associations uninitialized. The session is closed in a try-with-resources after the function returns.
      Type Parameters:
      T - result type
      Parameters:
      sessionFactory - the test session factory
      query - function that issues a query against the fresh session
      Returns:
      the value produced by query, with proxies detached