Search in sources :

Example 26 with Criteria

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

the class CriteriaManyToManyTest method testJoinTable.

@Test
public void testJoinTable() {
    Seller[] sellers = persist("join-table");
    Seller seller1 = sellers[0];
    Seller seller2 = sellers[1];
    Session session = openSession();
    Criteria criteria = session.createCriteria(Seller.class, "s");
    criteria.createCriteria("s.soldTo", "c", JoinType.INNER_JOIN, Restrictions.eq("name", "join-table-customer1"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked") List<Seller> results = criteria.list();
    assertTrue(results.size() == 1);
    assertTrue(results.contains(seller1));
    assertFalse(results.contains(seller2));
    criteria = session.createCriteria(Seller.class, "s");
    criteria.createCriteria("s.soldTo", "c", JoinType.INNER_JOIN, Restrictions.eq("name", "join-table-customer2"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked") List<Seller> results2 = criteria.list();
    assertTrue(results2.size() == 2);
    assertTrue(results2.contains(seller1));
    assertTrue(results2.contains(seller2));
    session.close();
}
Also used : Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 with Criteria

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

the class CriteriaManyToManyTest method testMappedBy.

@Test
public void testMappedBy() {
    Set<Customer> customersAll = new LinkedHashSet<Customer>();
    Seller[] sellers = persist("mappedby");
    customersAll.addAll(sellers[0].getSoldTo());
    customersAll.addAll(sellers[1].getSoldTo());
    Customer[] customers = customersAll.toArray(new Customer[customersAll.size()]);
    Customer customer1 = customers[0];
    Customer customer2 = customers[1];
    Customer customer3 = customers[2];
    Session session = openSession();
    Criteria criteria = session.createCriteria(Customer.class, "c");
    criteria.createCriteria("c.boughtFrom", "s", JoinType.INNER_JOIN, Restrictions.eq("name", "mappedby-seller1"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked") List<Customer> results = criteria.list();
    assertTrue(results.size() == 2);
    assertTrue(results.contains(customer1));
    assertTrue(results.contains(customer2));
    assertFalse(results.contains(customer3));
    criteria = session.createCriteria(Customer.class, "c");
    criteria.createCriteria("c.boughtFrom", "s", JoinType.INNER_JOIN, Restrictions.eq("name", "mappedby-seller2"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked") List<Customer> results2 = criteria.list();
    assertTrue(results2.size() == 2);
    assertFalse(results2.contains(customer1));
    assertTrue(results2.contains(customer2));
    assertTrue(results2.contains(customer3));
    session.close();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 28 with Criteria

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

the class NationalizedIgnoreCaseTest method testIgnoreCaseCriteria.

@Test
public void testIgnoreCaseCriteria() {
    User user1 = new User(1, "Chris");
    User user2 = new User(2, "Steve");
    doInHibernate(this::sessionFactory, session -> {
        session.save(user1);
        session.save(user2);
    });
    doInHibernate(this::sessionFactory, session -> {
        Criteria criteria = session.createCriteria(User.class);
        criteria.add(Restrictions.eq("name", user1.getName().toLowerCase()).ignoreCase());
        assertEquals(1, criteria.list().size());
    });
}
Also used : Criteria(org.hibernate.Criteria) Test(org.junit.Test)

Example 29 with Criteria

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

the class CriteriaLockingTest method testSetLockModeNONEDoNotLogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue.

@Test
@BMRules(rules = { @BMRule(targetClass = "org.hibernate.dialect.Dialect", targetMethod = "useFollowOnLocking", action = "return true", name = "H2DialectUseFollowOnLocking") })
public void testSetLockModeNONEDoNotLogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() {
    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.NONE);
    criteria.list();
    tx.rollback();
    s.close();
    releaseSessionFactory();
    assertFalse(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 30 with Criteria

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

the class CriteriaQueryTest method testClassProperty.

@Test
public void testClassProperty() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    // HQL: from Animal a where a.mother.class = Reptile
    Criteria c = s.createCriteria(Animal.class, "a").createAlias("mother", "m").add(Property.forName("m.class").eq(Reptile.class));
    c.list();
    t.rollback();
    s.close();
}
Also used : Reptile(org.hibernate.test.hql.Reptile) 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