use of org.hibernate.Criteria in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdUncached.
@Test
public void testNaturalIdUncached() {
saveSomeCitizens();
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = this.getState(s, "Ile de France");
Criteria criteria = s.createCriteria(Citizen.class);
criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
criteria.setCacheable(false);
this.cleanupCache();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
// first query
List results = criteria.list();
assertEquals(1, results.size());
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Query execution count should be one", 1, stats.getNaturalIdQueryExecutionCount());
// query a second time - result should be cached in session
criteria.list();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Second query should not be a miss", 1, stats.getNaturalIdCacheMissCount());
assertEquals("Query execution count should be one", 1, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.rollback();
s.close();
}
use of org.hibernate.Criteria in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdCached.
@Test
public void testNaturalIdCached() {
saveSomeCitizens();
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = this.getState(s, "Ile de France");
Criteria criteria = s.createCriteria(Citizen.class);
criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
criteria.setCacheable(true);
this.cleanupCache();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Cache puts should be empty", 0, stats.getNaturalIdCachePutCount());
// first query
List results = criteria.list();
assertEquals(1, results.size());
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());
// query a second time - result should be cached in session
criteria.list();
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.Criteria in project hibernate-orm by hibernate.
the class NaturalIdTest method getState.
private State getState(Session s, String name) {
Criteria criteria = s.createCriteria(State.class);
criteria.add(Restrictions.eq("name", name));
criteria.setCacheable(true);
return (State) criteria.list().get(0);
}
use of org.hibernate.Criteria in project hibernate-orm by hibernate.
the class CriteriaLockingTest method testSetLockModeDifferentFromNONELogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue.
@Test
@BMRules(rules = { @BMRule(targetClass = "org.hibernate.dialect.Dialect", targetMethod = "useFollowOnLocking", action = "return true", name = "H2DialectUseFollowOnLocking") })
public void testSetLockModeDifferentFromNONELogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() {
buildSessionFactory();
Triggerable triggerable = logInspection.watchForLogMessages("HHH000444");
final Session s = openSession();
final Transaction tx = s.beginTransaction();
Item item = new Item();
item.name = "ZZZZ";
s.persist(item);
s.flush();
Criteria criteria = s.createCriteria(Item.class).setLockMode(LockMode.OPTIMISTIC);
criteria.list();
tx.rollback();
s.close();
releaseSessionFactory();
assertTrue(triggerable.wasTriggered());
}
use of org.hibernate.Criteria in project hibernate-orm by hibernate.
the class CriteriaQueryTest method testClassProperty2.
@Test
public void testClassProperty2() {
Session session = openSession();
Transaction t = session.beginTransaction();
GreatFoo foo = new GreatFoo();
Bar b = new Bar();
b.setMyFoo(foo);
foo.setId(1);
b.setId(1);
session.persist(b);
session.flush();
t.commit();
session = openSession();
t = session.beginTransaction();
// OK, one BAR in DB
assertEquals(1, session.createCriteria(Bar.class).list().size());
Criteria crit = session.createCriteria(Bar.class, "b").createAlias("myFoo", "m").add(Property.forName("m.class").eq(GreatFoo.class));
assertEquals(1, crit.list().size());
crit = session.createCriteria(Bar.class, "b").createAlias("myFoo", "m").add(Restrictions.eq("m.class", GreatFoo.class));
assertEquals(1, crit.list().size());
t.commit();
session.close();
}
Aggregations