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;
}
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();
}
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);
}
Aggregations