Example usage for org.hibernate NaturalIdLoadAccess load

List of usage examples for org.hibernate NaturalIdLoadAccess load

Introduction

In this page you can find the example usage for org.hibernate NaturalIdLoadAccess load.

Prototype

T load();

Source Link

Document

Return the persistent instance with the natural id value(s) defined by the call(s) to #using , or null if there is no such persistent instance.

Usage

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testNaturalIdLoaderCached() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);// w w w  .  j av a  2s. co m
    stats.clear();

    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

    saveSomeCitizens();

    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

    //Try NaturalIdLoadAccess after insert
    final Citizen citizen = withTxSessionApply(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess<Citizen> naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);

        //Not clearing naturalId caches, should be warm from entity loading
        stats.clear();

        // first query
        Citizen c = naturalIdLoader.load();
        assertNotNull(c);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
        return c;
    });

    // TODO: Clear caches manually via cache manager (it's faster!!)
    cleanupCache();
    TIME_SERVICE.advance(1);
    stats.setStatisticsEnabled(true);
    stats.clear();

    //Try NaturalIdLoadAccess
    withTxSession(s -> {
        // first query
        Citizen loadedCitizen = (Citizen) s.get(Citizen.class, citizen.getId());
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });

    // Try NaturalIdLoadAccess after load
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);

        //Not clearing naturalId caches, should be warm from entity loading
        stats.setStatisticsEnabled(true);
        stats.clear();

        // first query
        Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });

}