Search in sources :

Example 61 with CriteriaBuilder

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

the class ListIndexTest method testListIndex.

@Test
@TestForIssue(jiraKey = "HHH-8404")
public void testListIndex() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address1 = new Address();
    address1.setId("a1");
    Phone phone1 = new Phone();
    phone1.setId("p1");
    phone1.setAddress(address1);
    Phone phone2 = new Phone();
    phone2.setId("p2");
    phone2.setAddress(address1);
    address1.getPhones().add(phone1);
    address1.getPhones().add(phone2);
    Address address2 = new Address();
    address2.setId("a2");
    Phone phone3 = new Phone();
    phone3.setId("p3");
    phone3.setAddress(address2);
    address2.getPhones().add(phone3);
    em.persist(phone1);
    em.persist(phone2);
    em.persist(phone3);
    em.persist(address1);
    em.persist(address2);
    em.getTransaction().commit();
    em.clear();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Address> criteria = cb.createQuery(Address.class);
    Root<Address> addressRoot = criteria.from(Address.class);
    ListJoin<Address, Phone> phones = addressRoot.join(Address_.phones);
    criteria.where(cb.gt(phones.index(), 0));
    List<Address> results = em.createQuery(criteria).getResultList();
    assertNotNull(results);
    // Ensure that the "index(phones) > 0" condition was included on the inner join, meaning only address1
    // (> 1 phone) was returned.
    assertEquals(1, results.size());
    assertEquals(address1.getId(), results.get(0).getId());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 62 with CriteriaBuilder

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

the class ComponentInWhereClauseTest method testEqualExpressionForThePropertyOfTheElementCollectionPropertyOfAComponent.

@Test
public void testEqualExpressionForThePropertyOfTheElementCollectionPropertyOfAComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
        Root<Employee> root = query.from(Employee.class);
        query.where(builder.equal(root.join("contactDetail").join("phones").get("number"), "+4411111111"));
        final List<Employee> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 63 with CriteriaBuilder

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

the class ComponentInWhereClauseTest method testEqualityForThePropertyOfAComponent.

@Test
public void testEqualityForThePropertyOfAComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
        Root<Employee> root = query.from(Employee.class);
        query.where(builder.equal(root.join("contactDetail").get("email"), "abc@mail.org"));
        final List<Employee> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 64 with CriteriaBuilder

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

the class EntitySuperclassComponentWithCollectionTest method testSizeExpressionForTheOneToManyPropertyOfAComponent.

@Test
public void testSizeExpressionForTheOneToManyPropertyOfAComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Manager> query = builder.createQuery(Manager.class);
        Root<Manager> root = query.from(Manager.class);
        query.where(builder.equal(builder.size(root.get("projects").get("previousProjects")), 2));
        final List<Manager> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 65 with CriteriaBuilder

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

the class MappedSuperclassComponentWithCollectionTest method testSizeExpressionForTheElementCollectionPropertyOfAComponent.

@Test
public void testSizeExpressionForTheElementCollectionPropertyOfAComponent() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Manager> query = builder.createQuery(Manager.class);
        Root<Manager> root = query.from(Manager.class);
        query.where(builder.equal(builder.size(root.get("contactDetail").get("phones")), 1));
        final List<Manager> results = entityManager.createQuery(query).getResultList();
        assertThat(results.size(), is(1));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2