HHH-11081 - Add test for issue

This commit is contained in:
Andrea Boriero 2016-09-12 23:56:34 +01:00 committed by Gail Badner
parent 6f43d40e0a
commit 4d3ece8c09
1 changed files with 107 additions and 16 deletions

View File

@ -6,6 +6,8 @@
*/ */
package org.hibernate.jpa.test.criteria; package org.hibernate.jpa.test.criteria;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -17,18 +19,19 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join; import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.util.List;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Before;
import org.junit.Test; 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 * @author Andrea Boriero
*/ */
@TestForIssue(jiraKey = "HHH-10844")
public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase { public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
@Override @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}; 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 @Test
@TestForIssue(jiraKey = "HHH-8488")
public void testTreatJoin() { public void testTreatJoin() {
EntityManager em = createEntityManager(); EntityManager em = createEntityManager();
try { try {
@ -48,7 +79,8 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
Join<Bid, Book> book = cb.treat( bid.join( "item" ), Book.class ); Join<Bid, Book> book = cb.treat( bid.join( "item" ), Book.class );
query.select( book.get( "title" ) ); query.select( book.get( "title" ) );
em.createQuery( query ).getResultList(); final List<Bid> resultList = em.createQuery( query ).getResultList();
assertThat(resultList.size(),is(2));
} }
finally { finally {
em.close(); em.close();
@ -56,6 +88,7 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8488")
public void testTreatJoin2() { public void testTreatJoin2() {
EntityManager em = createEntityManager(); EntityManager em = createEntityManager();
try { try {
@ -69,7 +102,8 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
query.select( bid ); query.select( bid );
em.createQuery( query ).getResultList(); final List<Bid> resultList = em.createQuery( query ).getResultList();
assertThat(resultList.size(),is(2));
} }
finally { finally {
em.close(); em.close();
@ -77,6 +111,7 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8488")
public void testJoinMethodOnATreatedJoin() { public void testJoinMethodOnATreatedJoin() {
EntityManager em = createEntityManager(); EntityManager em = createEntityManager();
try { try {
@ -91,32 +126,67 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
Join<Book, Author> owner = book.join( "author" ); Join<Book, Author> owner = book.join( "author" );
query.select( owner.get( "name" ) ); query.select( owner.get( "name" ) );
query.where( cb.equal( price.get("amount"), 1L ) ); query.where( cb.equal( price.get("amount"), 10 ) );
em.createQuery( query ).getResultList();
final List<Bid> resultList = em.createQuery( query ).getResultList();
assertThat(resultList.size(),is(2));
} }
finally { finally {
em.close(); em.close();
} }
} }
@Entity @Test
@TestForIssue( jiraKey = "HHH-11081")
public void testTreatedJoinInWhereClause() {
EntityManager em = createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Bid> query = cb.createQuery( Bid.class );
Root<Bid> bid = query.from( Bid.class );
final Join<Bid, Book> item = bid.join( "item" );
Join<Bid, Book> book = cb.treat( item, Book.class );
query.where( cb.equal( book.get("title"), "La moneta di Akragas" ) );
final List<Bid> resultList = em.createQuery( query ).getResultList();
assertThat(resultList.size(),is(1));
}
finally {
em.close();
}
}
@Entity(name = "Item")
public static class Item { public static class Item {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@ManyToOne @ManyToOne(cascade = CascadeType.PERSIST)
private Price price; private Price price;
public Item() {
}
public Item(Price price) {
this.price = price;
}
} }
@Entity @Entity(name = "Price")
public static class Price{ public static class Price{
@Id @Id
long id; long id;
public Price() {
}
public Price(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
int amount; int amount;
String currency; String currency;
@ -125,10 +195,19 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
@Entity(name = "Book") @Entity(name = "Book")
@Table(name="BOOK") @Table(name="BOOK")
public static class Book extends Item { public static class Book extends Item {
@ManyToOne private String title;
@ManyToOne(cascade = CascadeType.PERSIST)
private Author author; 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") @Entity(name = "Car")
@ -147,8 +226,15 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@ManyToOne @ManyToOne(cascade = CascadeType.PERSIST)
private Item item; private Item item;
public Bid() {
}
public Bid(Item item) {
this.item = item;
}
} }
@Entity(name = "Author") @Entity(name = "Author")
@ -159,6 +245,13 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
private Long id; private Long id;
private String name; private String name;
public Author() {
}
public Author(String name) {
this.name = name;
}
} }
@Entity(name = "Person") @Entity(name = "Person")
@ -170,6 +263,4 @@ public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
private String name; private String name;
} }
} }