Search in sources :

Example 1 with ThingWithQuantity

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

use of org.hibernate.jpa.test.metamodel.ThingWithQuantity 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)2 Thing (org.hibernate.jpa.test.metamodel.Thing)2 ThingWithQuantity (org.hibernate.jpa.test.metamodel.ThingWithQuantity)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Before (org.junit.Before)1 Test (org.junit.Test)1