Search in sources :

Example 46 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class TreatJoinTest method testJoinMethodOnATreatedJoin.

@Test
@TestForIssue(jiraKey = "HHH-8488")
public void testJoinMethodOnATreatedJoin() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Bid> query = cb.createQuery(Bid.class);
        Root<Bid> bid = query.from(Bid.class);
        final Join<Bid, Book> item = bid.join("item");
        final Join<Object, Object> price = item.join("price");
        Join<Bid, Book> book = cb.treat(item, Book.class);
        Join<Book, Author> owner = book.join("author");
        query.select(owner.get("name"));
        query.where(cb.equal(price.get("amount"), 10));
        final List<Bid> resultList = entityManager.createQuery(query).getResultList();
        assertThat(resultList.size(), is(2));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 47 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class TreatJoinTest method testJoinOnTreatedRootWithJoin.

@Test
@TestForIssue(jiraKey = "HHH-10561")
public void testJoinOnTreatedRootWithJoin() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Item> criteria = cb.createQuery(Item.class);
        Root<Item> root = criteria.from(Item.class);
        root.join("price");
        Root<Book> treatedRoot = cb.treat(root, Book.class);
        criteria.where(cb.equal(treatedRoot.<Book, Author>join("author").<String>get("name"), "Andrea Camilleri"));
        entityManager.createQuery(criteria.select(treatedRoot)).getResultList();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 48 with TestForIssue

use of org.hibernate.testing.TestForIssue 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 49 with TestForIssue

use of org.hibernate.testing.TestForIssue 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 TestForIssue

use of org.hibernate.testing.TestForIssue 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

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16