diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/TreatJoinTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/TreatJoinTest.java index 3566728db2..4b1ab26bab 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/TreatJoinTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/TreatJoinTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.jpa.test.criteria; +import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; @@ -17,18 +19,19 @@ import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Join; import javax.persistence.criteria.Root; -import java.util.List; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.TestForIssue; +import org.junit.Before; import org.junit.Test; -import org.hibernate.testing.TestForIssue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-10844") public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { @Override @@ -36,7 +39,35 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { return new Class[] {Item.class, Price.class, Book.class, Bid.class, Author.class, Car.class, Person.class}; } + @Before + public void setUp(){ + final EntityManager em = createEntityManager(); + try { + em.getTransaction().begin(); + try { + Price price = new Price( 10, "EUR" ); + Author author = new Author( "Andrea Camilleri" ); + Book book = new Book( author, "Il nipote del Negus", price ); + Bid bid = new Bid( book ); + em.persist( bid ); + + book = new Book( author, "La moneta di Akragas", price ); + bid = new Bid( book ); + em.persist( bid ); + + em.getTransaction().commit(); + }catch (Exception e){ + if(em.getTransaction().isActive()){ + em.getTransaction().rollback(); + } + } + }finally { + em.close(); + } + } + @Test + @TestForIssue(jiraKey = "HHH-8488") public void testTreatJoin() { EntityManager em = createEntityManager(); try { @@ -48,7 +79,8 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { Join book = cb.treat( bid.join( "item" ), Book.class ); query.select( book.get( "title" ) ); - em.createQuery( query ).getResultList(); + final List resultList = em.createQuery( query ).getResultList(); + assertThat(resultList.size(),is(2)); } finally { em.close(); @@ -56,6 +88,7 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { } @Test + @TestForIssue(jiraKey = "HHH-8488") public void testTreatJoin2() { EntityManager em = createEntityManager(); try { @@ -69,7 +102,8 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { query.select( bid ); - em.createQuery( query ).getResultList(); + final List resultList = em.createQuery( query ).getResultList(); + assertThat(resultList.size(),is(2)); } finally { em.close(); @@ -77,6 +111,7 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { } @Test + @TestForIssue(jiraKey = "HHH-8488") public void testJoinMethodOnATreatedJoin() { EntityManager em = createEntityManager(); try { @@ -91,32 +126,67 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { Join owner = book.join( "author" ); query.select( owner.get( "name" ) ); - query.where( cb.equal( price.get("amount"), 1L ) ); - - em.createQuery( query ).getResultList(); + query.where( cb.equal( price.get("amount"), 10 ) ); + final List resultList = em.createQuery( query ).getResultList(); + assertThat(resultList.size(),is(2)); } finally { em.close(); } } - @Entity + @Test + @TestForIssue( jiraKey = "HHH-11081") + public void testTreatedJoinInWhereClause() { + EntityManager em = createEntityManager(); + try { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery( Bid.class ); + Root bid = query.from( Bid.class ); + + final Join item = bid.join( "item" ); + Join book = cb.treat( item, Book.class ); + query.where( cb.equal( book.get("title"), "La moneta di Akragas" ) ); + + final List resultList = em.createQuery( query ).getResultList(); + assertThat(resultList.size(),is(1)); + } + finally { + em.close(); + } + } + + @Entity(name = "Item") public static class Item { @Id @GeneratedValue private Long id; - @ManyToOne + @ManyToOne(cascade = CascadeType.PERSIST) private Price price; + public Item() { + } + + public Item(Price price) { + this.price = price; + } } - @Entity + @Entity(name = "Price") public static class Price{ @Id long id; + public Price() { + } + + public Price(int amount, String currency) { + this.amount = amount; + this.currency = currency; + } + int amount; String currency; @@ -125,10 +195,19 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { @Entity(name = "Book") @Table(name="BOOK") public static class Book extends Item { - @ManyToOne + private String title; + + @ManyToOne(cascade = CascadeType.PERSIST) private Author author; - String title; + public Book() { + } + + public Book(Author author, String title, Price price) { + super(price); + this.author = author; + this.title = title; + } } @Entity(name = "Car") @@ -147,8 +226,15 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { @GeneratedValue private Long id; - @ManyToOne + @ManyToOne(cascade = CascadeType.PERSIST) private Item item; + + public Bid() { + } + + public Bid(Item item) { + this.item = item; + } } @Entity(name = "Author") @@ -159,6 +245,13 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { private Long id; private String name; + + public Author() { + } + + public Author(String name) { + this.name = name; + } } @Entity(name = "Person") @@ -170,6 +263,4 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { private String name; } - - }