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