Search in sources :

Example 46 with EntityManager

use of javax.persistence.EntityManager 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 47 with EntityManager

use of javax.persistence.EntityManager 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 48 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class TreatKeywordTest method treatPathClassTestHqlControl.

@Test
public void treatPathClassTestHqlControl() {
    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();
    List<String> animalList = em.createQuery("select a.name from Animal a where treat (a as Human).name like '2%'").getResultList();
    Assert.assertEquals("treat(Animal as Human) was ignored", 1, animalList.size());
    List<Long> animalIdList = em.createQuery("select a.id from Animal a where treat (a as Human).name like '2%'").getResultList();
    Assert.assertEquals("treat(Animal as Human) was ignored", 1, animalList.size());
    Assert.assertEquals(200L, animalIdList.get(0).longValue());
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 49 with EntityManager

use of javax.persistence.EntityManager 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 50 with EntityManager

use of javax.persistence.EntityManager 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)

Aggregations

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35