HHH-11081 - Add test for issue

(cherry picked from commit 4d3ece8c09)
(cherry picked from commit e0a9f51ac7)
This commit is contained in:
Andrea Boriero 2016-09-12 23:56:34 +01:00 committed by Gail Badner
parent b809eb09b9
commit 5bb5931f38
1 changed files with 107 additions and 16 deletions

View File

@ -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<Bid, Book> book = cb.treat( bid.join( "item" ), Book.class );
query.select( book.get( "title" ) );
em.createQuery( query ).getResultList();
final List<Bid> 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<Bid> 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<Book, Author> 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<Bid> 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<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 {
@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;
}
}