HHH-8488 - Add tests for issue
This commit is contained in:
parent
cf0fb8d262
commit
9d20d3ab03
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.jpa.test.criteria;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
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.junit.Test;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-10844")
|
||||
public class TreatJoinTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {Item.class, Price.class, Book.class, Bid.class, Author.class, Car.class, Person.class};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTreatJoin() {
|
||||
EntityManager em = createEntityManager();
|
||||
try {
|
||||
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Bid> query = cb.createQuery( Bid.class );
|
||||
Root<Bid> bid = query.from( Bid.class );
|
||||
|
||||
Join<Bid, Book> book = cb.treat( bid.join( "item" ), Book.class );
|
||||
query.select( book.get( "title" ) );
|
||||
|
||||
em.createQuery( query ).getResultList();
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTreatJoin2() {
|
||||
EntityManager em = createEntityManager();
|
||||
try {
|
||||
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Bid> query = cb.createQuery( Bid.class );
|
||||
Root<Bid> bid = query.from( Bid.class );
|
||||
|
||||
cb.treat( bid.join( "item" ), Book.class );
|
||||
cb.treat( bid.join( "item" ), Car.class );
|
||||
|
||||
query.select( bid );
|
||||
|
||||
em.createQuery( query ).getResultList();
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinMethodOnATreatedJoin() {
|
||||
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" );
|
||||
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"), 1L ) );
|
||||
|
||||
em.createQuery( query ).getResultList();
|
||||
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Item {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private Price price;
|
||||
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Price{
|
||||
@Id
|
||||
long id;
|
||||
|
||||
int amount;
|
||||
|
||||
String currency;
|
||||
}
|
||||
|
||||
@Entity(name = "Book")
|
||||
@Table(name="BOOK")
|
||||
public static class Book extends Item {
|
||||
@ManyToOne
|
||||
private Author author;
|
||||
|
||||
String title;
|
||||
}
|
||||
|
||||
@Entity(name = "Car")
|
||||
@Table(name="CAR")
|
||||
public static class Car extends Item {
|
||||
@OneToMany
|
||||
private List<Person> owners;
|
||||
|
||||
String color;
|
||||
}
|
||||
|
||||
@Entity(name = "Bid")
|
||||
@Table(name = "BID")
|
||||
public static class Bid {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private Item item;
|
||||
}
|
||||
|
||||
@Entity(name = "Author")
|
||||
@Table(name = "AUTHOR")
|
||||
public static class Author {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
@Table(name = "PERSON")
|
||||
public static class Person {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.jpa.test.criteria;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Tuple;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.ListJoin;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.Selection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class TreatListJoinTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {TestEntity.class, EntityA.class, EntityB.class};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EntityManager em = createEntityManager();
|
||||
try {
|
||||
em.getTransaction().begin();
|
||||
|
||||
for ( int i = 0; i < 5; i++ ) {
|
||||
TestEntity e = new TestEntity();
|
||||
EntityA eA = new EntityA();
|
||||
eA.setParent( e );
|
||||
eA.valueA = "a_" + i;
|
||||
|
||||
em.persist( e );
|
||||
}
|
||||
for ( int i = 0; i < 5; i++ ) {
|
||||
TestEntity e = new TestEntity();
|
||||
|
||||
EntityB eB = new EntityB();
|
||||
eB.valueB = "b_" + i;
|
||||
eB.setParent( e );
|
||||
|
||||
em.persist( e );
|
||||
}
|
||||
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( em.getTransaction().isActive() ) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTreatJoin() {
|
||||
EntityManager em = createEntityManager();
|
||||
try {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
|
||||
final CriteriaQuery<Tuple> query = cb.createTupleQuery();
|
||||
final Root<TestEntity> testEntity = query.from( TestEntity.class );
|
||||
|
||||
final List<Selection<?>> selections = new LinkedList();
|
||||
selections.add( testEntity.get( "id" ) );
|
||||
|
||||
final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList(
|
||||
"entities",
|
||||
JoinType.LEFT
|
||||
);
|
||||
entities.on( cb.equal( entities.get( "entityType" ), EntityA.class.getName() ) );
|
||||
|
||||
final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(
|
||||
entities,
|
||||
EntityA.class
|
||||
);
|
||||
selections.add( joinEntityA.get( "id" ) );
|
||||
selections.add( joinEntityA.get( "valueA" ) );
|
||||
|
||||
final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList(
|
||||
"entities",
|
||||
JoinType.LEFT
|
||||
);
|
||||
entitiesB.on( cb.equal( entitiesB.get( "entityType" ), EntityB.class.getName() ) );
|
||||
final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(
|
||||
entitiesB,
|
||||
EntityB.class
|
||||
);
|
||||
selections.add( joinEntityB.get( "id" ) );
|
||||
selections.add( joinEntityB.get( "valueB" ) );
|
||||
|
||||
query.multiselect( selections );
|
||||
|
||||
final List<Tuple> resultList = em.createQuery( query ).getResultList();
|
||||
assertThat( resultList.size(), is( 10 ) );
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@MappedSuperclass
|
||||
public static abstract class MyEntity {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
}
|
||||
|
||||
@Entity(name = "TestEntity")
|
||||
public static class TestEntity extends MyEntity {
|
||||
@OneToMany(mappedBy = "parent", cascade = {CascadeType.ALL})
|
||||
private List<AbstractEntity> entities = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity(name = "AbstractEntity")
|
||||
public static abstract class AbstractEntity extends MyEntity {
|
||||
String entityType = getClass().getName();
|
||||
|
||||
@ManyToOne
|
||||
private TestEntity parent;
|
||||
|
||||
public TestEntity getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TestEntity parent) {
|
||||
this.parent = parent;
|
||||
parent.entities.add( this );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "EntityA")
|
||||
public static class EntityA extends AbstractEntity {
|
||||
public String valueA;
|
||||
|
||||
public EntityA() {
|
||||
super.entityType = getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "EntityB")
|
||||
public static class EntityB extends AbstractEntity {
|
||||
public String valueB;
|
||||
|
||||
public EntityB() {
|
||||
super.entityType = getClass().getName();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue