Search in sources :

Example 1 with Predicate

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

the class CriteriaTest method test_criteria_from_multiple_root_example.

@Test
public void test_criteria_from_multiple_root_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        String address = "Earth";
        String prefix = "J%";
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        Root<Person> personRoot = criteria.from(Person.class);
        Root<Partner> partnerRoot = criteria.from(Partner.class);
        criteria.multiselect(personRoot, partnerRoot);
        Predicate personRestriction = builder.and(builder.equal(personRoot.get(Person_.address), address), builder.isNotEmpty(personRoot.get(Person_.phones)));
        Predicate partnerRestriction = builder.and(builder.like(partnerRoot.get(Partner_.name), prefix), builder.equal(partnerRoot.get(Partner_.version), 0));
        criteria.where(builder.and(personRestriction, partnerRestriction));
        List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
        assertEquals(2, tuples.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Partner(org.hibernate.userguide.model.Partner) Tuple(javax.persistence.Tuple) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 2 with Predicate

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

the class ExpressionsTest method testParameterReuse.

@Test
public void testParameterReuse() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = em.getCriteriaBuilder().createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    ParameterExpression<String> param = em.getCriteriaBuilder().parameter(String.class);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(Product_.id), param);
    Predicate predicate2 = em.getCriteriaBuilder().equal(from.get(Product_.name), param);
    criteria.where(em.getCriteriaBuilder().or(predicate, predicate2));
    assertEquals(1, criteria.getParameters().size());
    TypedQuery<Product> query = em.createQuery(criteria);
    int hqlParamCount = countGeneratedParameters(query.unwrap(Query.class));
    assertEquals(1, hqlParamCount);
    query.setParameter(param, "abc").getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TypedQuery(javax.persistence.TypedQuery) Query(org.hibernate.Query) Product(org.hibernate.jpa.test.metamodel.Product) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 3 with Predicate

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

the class BasicCriteriaUsageTest method testDateCompositeCustomType.

@Test
@TestForIssue(jiraKey = "HHH-8283")
public void testDateCompositeCustomType() {
    Payment payment = new Payment();
    payment.setAmount(new BigDecimal(1000));
    payment.setDate(new Date());
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(payment);
    CriteriaQuery<Payment> criteria = em.getCriteriaBuilder().createQuery(Payment.class);
    Root<Payment> rp = criteria.from(Payment.class);
    Predicate predicate = em.getCriteriaBuilder().equal(rp.get(Payment_.date), new Date());
    criteria.where(predicate);
    TypedQuery<Payment> q = em.createQuery(criteria);
    List<Payment> payments = q.getResultList();
    assertEquals(1, payments.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with Predicate

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

the class BasicCriteriaUsageTest method testParameterCollection.

@Test
public void testParameterCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery(Wall.class);
    Root<Wall> from = criteria.from(Wall.class);
    ParameterExpression param = em.getCriteriaBuilder().parameter(String.class);
    SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity(Wall.class).getDeclaredSingularAttribute("color");
    assertNotNull("metamodel returned null singular attribute", colorAttribute);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(colorAttribute), param);
    criteria.where(predicate);
    assertEquals(1, criteria.getParameters().size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) ParameterExpression(javax.persistence.criteria.ParameterExpression) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 5 with Predicate

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

the class SelectCaseTest method selectCaseWithValuesShouldWork.

@Test
public void selectCaseWithValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.Case<EnumValue> selectCase = cb.selectCase();
    Predicate somePredicate = cb.equal(cb.literal(1), 1);
    selectCase.when(somePredicate, EnumValue.VALUE_1);
    selectCase.otherwise(EnumValue.VALUE_2);
    CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
    Root<Entity> from = query.from(Entity.class);
    query.select(from).where(cb.equal(from.get("value"), selectCase));
    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)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