Search in sources :

Example 6 with Predicate

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();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 7 with Predicate

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();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 8 with Predicate

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();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 9 with Predicate

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();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 10 with Predicate

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();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

Predicate (javax.persistence.criteria.Predicate)35 EntityManager (javax.persistence.EntityManager)19 Test (org.junit.Test)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)7 DAOException (org.jbei.ice.storage.DAOException)7 ArrayList (java.util.ArrayList)4 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)2 Map (java.util.Map)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)2 Request (org.jbei.ice.storage.model.Request)2 Function (com.google.common.base.Function)1 Transactional (com.google.inject.persist.Transactional)1 DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1