use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testComplicatedNotAnd.
/**
* Check complicated not.
*/
@Test
public void testComplicatedNotAnd() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
Predicate p1 = builder.equal(orderRoot.get("id"), "order-1");
Predicate p2 = builder.equal(orderRoot.get("id"), "order-2");
Predicate compoundPredicate = builder.and(p1, p2).not();
// a negated AND should become an OR
assertEquals(Predicate.BooleanOperator.OR, compoundPredicate.getOperator());
orderCriteria.where(compoundPredicate);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertEquals(3, orders.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testSimpleNot2.
/**
* Check simple not.
*/
@Test
public void testSimpleNot2() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
final Predicate p = builder.equal(orderRoot.get("id"), "order-1").not();
assertEquals(Predicate.BooleanOperator.AND, p.getOperator());
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertEquals(2, orders.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testByteArray.
/**
* Check predicate for field which has simple char array type (byte[]).
*/
@Test
public void testByteArray() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
Predicate p = builder.equal(orderRoot.get("number"), new byte[] { '1', '2' });
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertTrue(orders.size() == 0);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testCharArray.
/**
* Check predicate for field which has simple char array type (char[]).
*/
@Test
public void testCharArray() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
Predicate p = builder.equal(orderRoot.get("domen"), new char[] { 'r', 'u' });
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertTrue(orders.size() == 1);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testSimpleNot.
/**
* Check simple not.
*/
@Test
public void testSimpleNot() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
final Predicate p = builder.not(builder.equal(orderRoot.get("id"), "order-1"));
assertEquals(Predicate.BooleanOperator.AND, p.getOperator());
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertEquals(2, orders.size());
em.getTransaction().commit();
em.close();
}
Aggregations