Search in sources :

Example 21 with Criteria

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with Criteria

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with Criteria

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);
}
Also used : Criteria(org.hibernate.Criteria)

Example 24 with Criteria

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());
}
Also used : Transaction(org.hibernate.Transaction) Triggerable(org.hibernate.testing.logger.Triggerable) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 25 with Criteria

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();
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Criteria (org.hibernate.Criteria)174 Session (org.hibernate.Session)92 Test (org.junit.Test)69 Transaction (org.hibernate.Transaction)39 List (java.util.List)35 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)27 ArrayList (java.util.ArrayList)15 TestForIssue (org.hibernate.testing.TestForIssue)12 Iterator (java.util.Iterator)9 Period (org.hisp.dhis.period.Period)8 Map (java.util.Map)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)5 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)4 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)4 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Example (org.hibernate.criterion.Example)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2