Search in sources :

Example 16 with CriteriaBuilder

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

the class TreatKeywordTest method basicTest.

@Test
public void basicTest() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Thing> criteria = builder.createQuery(Thing.class);
    Root<Thing> root = criteria.from(Thing.class);
    criteria.select(root);
    criteria.where(builder.equal(builder.treat(root, ThingWithQuantity.class).get(ThingWithQuantity_.quantity), 2));
    em.createQuery(criteria).getResultList();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ThingWithQuantity(org.hibernate.jpa.test.metamodel.ThingWithQuantity) Thing(org.hibernate.jpa.test.metamodel.Thing) Test(org.junit.Test)

Example 17 with CriteriaBuilder

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

the class TreatKeywordTest method testTreatWithRestrictionOnAbstractClass.

@Test
@TestForIssue(jiraKey = "HHH-9411")
public void testTreatWithRestrictionOnAbstractClass() {
    EntityManager em = getOrCreateEntityManager();
    EntityTransaction entityTransaction = em.getTransaction();
    entityTransaction.begin();
    Greyhound greyhound = new Greyhound();
    Dachshund dachshund = new Dachshund();
    em.persist(greyhound);
    em.persist(dachshund);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TreatAnimal> criteriaQuery = cb.createQuery(TreatAnimal.class);
    Root<TreatAnimal> animal = criteriaQuery.from(TreatAnimal.class);
    Root<Dog> dog = cb.treat(animal, Dog.class);
    // only fast dogs
    criteriaQuery.where(cb.isTrue(dog.<Boolean>get("fast")));
    List<TreatAnimal> results = em.createQuery(criteriaQuery).getResultList();
    // we should only have a single Greyhound here, not slow long dogs!
    assertEquals(Arrays.asList(greyhound), results);
    entityTransaction.commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with CriteriaBuilder

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

the class TreatKeywordTest method treatRoot.

@Test
@TestForIssue(jiraKey = "HHH-9549")
public void treatRoot() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Animal animal = new Animal();
    animal.setId(100L);
    animal.setName("2");
    em.persist(animal);
    Human human = new Human();
    human.setId(200L);
    human.setName("2");
    em.persist(human);
    Elephant elephant = new Elephant();
    elephant.setId(300L);
    elephant.setName("2");
    em.persist(elephant);
    em.getTransaction().commit();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Human> criteria = builder.createQuery(Human.class);
    Root<Animal> root = criteria.from(Animal.class);
    criteria.select(builder.treat(root, Human.class));
    List<Human> humans = em.createQuery(criteria).getResultList();
    Assert.assertEquals(1, humans.size());
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with CriteriaBuilder

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

the class TreatKeywordTest method treatRootReturnSuperclass.

@Test
@TestForIssue(jiraKey = "HHH-9549")
public void treatRootReturnSuperclass() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Animal animal = new Animal();
    animal.setId(100L);
    animal.setName("2");
    em.persist(animal);
    Human human = new Human();
    human.setId(200L);
    human.setName("2");
    em.persist(human);
    Elephant elephant = new Elephant();
    elephant.setId(300L);
    elephant.setName("2");
    em.persist(elephant);
    em.getTransaction().commit();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Animal> criteria = builder.createQuery(Animal.class);
    Root<Animal> root = criteria.from(Animal.class);
    criteria.select(builder.treat(root, Human.class));
    List<Animal> animalsThatAreHuman = em.createQuery(criteria).getResultList();
    Assert.assertEquals(1, animalsThatAreHuman.size());
    Assert.assertTrue(Human.class.isInstance(animalsThatAreHuman.get(0)));
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 20 with CriteriaBuilder

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

the class TreatListJoinTest method testTreatJoin.

@Test
public void testTreatJoin() {
    EntityManager em = createEntityManager();
    try {
        final CriteriaBuilder cb = em.getCriteriaBuilder();
        final CriteriaQuery<Tuple> query = cb.createTupleQuery();
        final Root<TestEntity> testEntity = query.from(TestEntity.class);
        final List<Selection<?>> selections = new LinkedList();
        selections.add(testEntity.get("id"));
        final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList("entities", JoinType.LEFT);
        entities.on(cb.equal(entities.get("entityType"), EntityA.class.getName()));
        final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(entities, EntityA.class);
        selections.add(joinEntityA.get("id"));
        selections.add(joinEntityA.get("valueA"));
        final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList("entities", JoinType.LEFT);
        entitiesB.on(cb.equal(entitiesB.get("entityType"), EntityB.class.getName()));
        final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(entitiesB, EntityB.class);
        selections.add(joinEntityB.get("id"));
        selections.add(joinEntityB.get("valueB"));
        query.multiselect(selections);
        final List<Tuple> resultList = em.createQuery(query).getResultList();
        assertThat(resultList.size(), is(10));
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Selection(javax.persistence.criteria.Selection) LinkedList(java.util.LinkedList) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) 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