Search in sources :

Example 36 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class PredicateTest method testQuotientConversion.

@Test
@TestForIssue(jiraKey = "HHH-5803")
public void testQuotientConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    Long longValue = 999999999L;
    Path<Double> doublePath = orderRoot.get(Order_.totalPrice);
    Path<Integer> integerPath = orderRoot.get(Order_.customer).get(Customer_.age);
    orderCriteria.select(orderRoot);
    Predicate p = builder.ge(builder.quot(integerPath, doublePath), longValue);
    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) TestForIssue(org.hibernate.testing.TestForIssue)

Example 37 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class PredicateTest method testComplicatedNotOr.

/**
	 * Check complicated not.
	 */
@Test
public void testComplicatedNotOr() {
    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.not(builder.or(p1, p2));
    // negated OR should become an AND
    assertEquals(Predicate.BooleanOperator.AND, compoundPredicate.getOperator());
    orderCriteria.where(compoundPredicate);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(1, orders.size());
    Order order = orders.get(0);
    assertEquals("order-3", order.getId());
    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 38 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class IdClassPredicateTest method testDeclaredIdClassAttributes.

@Test
public void testDeclaredIdClassAttributes() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // Packaging arguments for use in query.
    List<String> divisions = new ArrayList<String>();
    divisions.add("NA");
    divisions.add("EU");
    // Building the query.
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Widget> query = criteria.createQuery(Widget.class);
    Root<Widget> root = query.from(Widget.class);
    Predicate predicate = root.get("division").in(divisions);
    query.where(predicate);
    // Retrieving query.;
    List<Widget> widgets = em.createQuery(query).getResultList();
    Assert.assertEquals(4, widgets.size());
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 39 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class IdClassPredicateTest method testSupertypeIdClassAttributes.

@Test
public void testSupertypeIdClassAttributes() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // Packaging arguments for use in query.
    List<String> types = new ArrayList<String>();
    types.add("NA");
    types.add("EU");
    // Building the query.
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Tool> query = criteria.createQuery(Tool.class);
    Root<Tool> root = query.from(Tool.class);
    Predicate predicate = root.get("type").in(types);
    query.where(predicate);
    // Retrieving query.
    List<Tool> tools = em.createQuery(query).getResultList();
    Assert.assertEquals(4, tools.size());
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 40 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class SelectCaseTest method selectCaseWithCastedTypeValuesShouldWork.

@Test
public void selectCaseWithCastedTypeValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.Case<String> selectCase = cb.selectCase();
    Predicate somePredicate = cb.equal(cb.literal(1), 1);
    selectCase.when(somePredicate, EnumValue.VALUE_1.name());
    selectCase.otherwise(EnumValue.VALUE_2.name());
    CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
    Root<Entity> from = query.from(Entity.class);
    query.select(from).where(cb.equal(from.get("value"), selectCase.as(String.class)));
    entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Aggregations

Predicate (javax.persistence.criteria.Predicate)59 EntityManager (javax.persistence.EntityManager)19 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)19 Test (org.junit.Test)17 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 ArrayList (java.util.ArrayList)9 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)6 DAOException (org.jbei.ice.storage.DAOException)6 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)5 Root (javax.persistence.criteria.Root)5 LinkedList (java.util.LinkedList)3 List (java.util.List)3 TypedQuery (javax.persistence.TypedQuery)3 Study (com.odysseusinc.arachne.portal.model.Study)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)2 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)2 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)2 IdmRoleCatalogueRole (eu.bcvsolutions.idm.core.model.entity.IdmRoleCatalogueRole)2 IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)2