Search in sources :

Example 1 with Thing

use of org.hibernate.jpa.test.metamodel.Thing 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 2 with Thing

use of org.hibernate.jpa.test.metamodel.Thing in project hibernate-orm by hibernate.

the class AbstractPathImplTest method prepareTestData.

@Before
public void prepareTestData() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Thing thing = new Thing();
    thing.setId("thing1");
    thing.setName("A Thing");
    em.persist(thing);
    thing = new Thing();
    thing.setId("thing2");
    thing.setName("Another Thing");
    em.persist(thing);
    ThingWithQuantity thingWithQuantity = new ThingWithQuantity();
    thingWithQuantity.setId("thingWithQuantity3");
    thingWithQuantity.setName("3 Things");
    thingWithQuantity.setQuantity(3);
    em.persist(thingWithQuantity);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) ThingWithQuantity(org.hibernate.jpa.test.metamodel.ThingWithQuantity) Thing(org.hibernate.jpa.test.metamodel.Thing) Before(org.junit.Before)

Example 3 with Thing

use of org.hibernate.jpa.test.metamodel.Thing in project hibernate-orm by hibernate.

the class AbstractPathImplTest method testTypeExpression.

@Test
public void testTypeExpression() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Thing> criteria = criteriaBuilder.createQuery(Thing.class);
        Root<Thing> thingRoot = criteria.from(Thing.class);
        criteria.select(thingRoot);
        assertEquals(em.createQuery(criteria).getResultList().size(), 3);
        criteria.where(criteriaBuilder.equal(thingRoot.type(), criteriaBuilder.literal(Thing.class)));
        assertEquals(em.createQuery(criteria).getResultList().size(), 2);
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Thing(org.hibernate.jpa.test.metamodel.Thing) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 4 with Thing

use of org.hibernate.jpa.test.metamodel.Thing in project hibernate-orm by hibernate.

the class TreatKeywordTest method testSelectSubclassPropertyFromDowncast.

@Test
public void testSelectSubclassPropertyFromDowncast() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
    Root<Thing> root = criteria.from(Thing.class);
    Root<ThingWithQuantity> subroot = builder.treat(root, ThingWithQuantity.class);
    criteria.select(subroot.<Integer>get("quantity"));
    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)

Aggregations

EntityManager (javax.persistence.EntityManager)4 Thing (org.hibernate.jpa.test.metamodel.Thing)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ThingWithQuantity (org.hibernate.jpa.test.metamodel.ThingWithQuantity)3 Test (org.junit.Test)3 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)1 Before (org.junit.Before)1