Search in sources :

Example 6 with NaturalIdLoadAccess

use of org.hibernate.NaturalIdLoadAccess in project hibernate-orm by hibernate.

the class SessionImpl method list.

@Override
public List list(Criteria criteria) throws HibernateException {
    // TODO: Is this guaranteed to always be CriteriaImpl?
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    if (criteriaImpl.getMaxResults() != null && criteriaImpl.getMaxResults() == 0) {
        return Collections.EMPTY_LIST;
    }
    final NaturalIdLoadAccess naturalIdLoadAccess = this.tryNaturalIdLoadAccess(criteriaImpl);
    if (naturalIdLoadAccess != null) {
        // EARLY EXIT!
        return Arrays.asList(naturalIdLoadAccess.load());
    }
    checkOpenOrWaitingForAutoClose();
    //		checkTransactionSynchStatus();
    String[] implementors = getFactory().getMetamodel().getImplementors(criteriaImpl.getEntityOrClassName());
    int size = implementors.length;
    CriteriaLoader[] loaders = new CriteriaLoader[size];
    Set spaces = new HashSet();
    for (int i = 0; i < size; i++) {
        loaders[i] = new CriteriaLoader(getOuterJoinLoadable(implementors[i]), getFactory(), criteriaImpl, implementors[i], getLoadQueryInfluencers());
        spaces.addAll(loaders[i].getQuerySpaces());
    }
    autoFlushIfRequired(spaces);
    List results = Collections.EMPTY_LIST;
    dontFlushFromFind++;
    boolean success = false;
    try {
        for (int i = 0; i < size; i++) {
            final List currentResults = loaders[i].list(this);
            currentResults.addAll(results);
            results = currentResults;
        }
        success = true;
    } finally {
        dontFlushFromFind--;
        afterOperation(success);
        delayedAfterCompletion();
    }
    return results;
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) SimpleNaturalIdLoadAccess(org.hibernate.SimpleNaturalIdLoadAccess) Set(java.util.Set) HashSet(java.util.HashSet) CriteriaLoader(org.hibernate.loader.criteria.CriteriaLoader) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 7 with NaturalIdLoadAccess

use of org.hibernate.NaturalIdLoadAccess in project hibernate-orm by hibernate.

the class NaturalIdTest method testNaturalIdLoaderNotCached.

@Test
public void testNaturalIdLoaderNotCached() {
    saveSomeCitizens();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    State france = this.getState(s, "Ile de France");
    final NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
    naturalIdLoader.using("ssn", "1234").using("state", france);
    //NaturalId cache gets populated during entity loading, need to clear it out
    this.cleanupCache();
    Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);
    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());
    // first query
    Citizen citizen = (Citizen) naturalIdLoader.load();
    assertNotNull(citizen);
    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
    // cleanup
    tx.rollback();
    s.close();
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) Transaction(org.hibernate.Transaction) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with NaturalIdLoadAccess

use of org.hibernate.NaturalIdLoadAccess in project uPortal by Jasig.

the class BaseJpaDao method createNaturalIdQuery.

/**
   * Utility for creating queries based on naturalId. The caller MUST be annotated with {@link
   * org.apereo.portal.jpa.OpenEntityManager} or {@link Transactional} so that the Hibernate
   * specific extensions are available.
   */
protected final <T> NaturalIdQuery<T> createNaturalIdQuery(Class<T> entityType) {
    final EntityManager entityManager = this.getEntityManager();
    final Session session;
    try {
        session = entityManager.unwrap(Session.class);
    } catch (IllegalStateException e) {
        throw new IllegalStateException("The DAO Method that calls createNaturalIdQuery must be annotated with @OpenEntityManager or @Transactional", e);
    }
    final NaturalIdLoadAccess naturalIdLoadAccess = session.byNaturalId(entityType);
    return new NaturalIdQuery<T>(entityType, naturalIdLoadAccess);
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) EntityManager(javax.persistence.EntityManager) Session(org.hibernate.Session)

Aggregations

NaturalIdLoadAccess (org.hibernate.NaturalIdLoadAccess)8 Session (org.hibernate.Session)5 Test (org.junit.Test)5 Transaction (org.hibernate.Transaction)4 Statistics (org.hibernate.stat.Statistics)4 SimpleNaturalIdLoadAccess (org.hibernate.SimpleNaturalIdLoadAccess)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 EntityManager (javax.persistence.EntityManager)1 NaturalIdentifier (org.hibernate.criterion.NaturalIdentifier)1 CriterionEntry (org.hibernate.internal.CriteriaImpl.CriterionEntry)1 CriteriaLoader (org.hibernate.loader.criteria.CriteriaLoader)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 Citizen (org.hibernate.test.cache.infinispan.functional.entities.Citizen)1 State (org.hibernate.test.cache.infinispan.functional.entities.State)1 TestForIssue (org.hibernate.testing.TestForIssue)1