|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.jpa.test.criteria; |
| 8 | + |
| 9 | +import javax.persistence.CascadeType; |
| 10 | +import javax.persistence.Entity; |
| 11 | +import javax.persistence.EntityManager; |
| 12 | +import javax.persistence.GeneratedValue; |
| 13 | +import javax.persistence.Id; |
| 14 | +import javax.persistence.ManyToOne; |
| 15 | +import javax.persistence.MappedSuperclass; |
| 16 | +import javax.persistence.OneToMany; |
| 17 | +import javax.persistence.Tuple; |
| 18 | +import javax.persistence.criteria.CriteriaBuilder; |
| 19 | +import javax.persistence.criteria.CriteriaQuery; |
| 20 | +import javax.persistence.criteria.JoinType; |
| 21 | +import javax.persistence.criteria.ListJoin; |
| 22 | +import javax.persistence.criteria.Root; |
| 23 | +import javax.persistence.criteria.Selection; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.LinkedList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 29 | + |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import static org.hamcrest.core.Is.is; |
| 34 | +import static org.junit.Assert.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Andrea Boriero |
| 38 | + */ |
| 39 | +public class TreatListJoinTest extends BaseEntityManagerFunctionalTestCase { |
| 40 | + |
| 41 | + @Override |
| 42 | + protected Class<?>[] getAnnotatedClasses() { |
| 43 | + return new Class[] {TestEntity.class, EntityA.class, EntityB.class}; |
| 44 | + } |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + EntityManager em = createEntityManager(); |
| 49 | + try { |
| 50 | + em.getTransaction().begin(); |
| 51 | + |
| 52 | + for ( int i = 0; i < 5; i++ ) { |
| 53 | + TestEntity e = new TestEntity(); |
| 54 | + EntityA eA = new EntityA(); |
| 55 | + eA.setParent( e ); |
| 56 | + eA.valueA = "a_" + i; |
| 57 | + |
| 58 | + em.persist( e ); |
| 59 | + } |
| 60 | + for ( int i = 0; i < 5; i++ ) { |
| 61 | + TestEntity e = new TestEntity(); |
| 62 | + |
| 63 | + EntityB eB = new EntityB(); |
| 64 | + eB.valueB = "b_" + i; |
| 65 | + eB.setParent( e ); |
| 66 | + |
| 67 | + em.persist( e ); |
| 68 | + } |
| 69 | + |
| 70 | + em.getTransaction().commit(); |
| 71 | + } |
| 72 | + catch (Exception e) { |
| 73 | + if ( em.getTransaction().isActive() ) { |
| 74 | + em.getTransaction().rollback(); |
| 75 | + } |
| 76 | + throw e; |
| 77 | + } |
| 78 | + finally { |
| 79 | + em.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testTreatJoin() { |
| 85 | + EntityManager em = createEntityManager(); |
| 86 | + try { |
| 87 | + final CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 88 | + |
| 89 | + final CriteriaQuery<Tuple> query = cb.createTupleQuery(); |
| 90 | + final Root<TestEntity> testEntity = query.from( TestEntity.class ); |
| 91 | + |
| 92 | + final List<Selection<?>> selections = new LinkedList(); |
| 93 | + selections.add( testEntity.get( "id" ) ); |
| 94 | + |
| 95 | + final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList( |
| 96 | + "entities", |
| 97 | + JoinType.LEFT |
| 98 | + ); |
| 99 | + entities.on( cb.equal( entities.get( "entityType" ), EntityA.class.getName() ) ); |
| 100 | + |
| 101 | + final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat( |
| 102 | + entities, |
| 103 | + EntityA.class |
| 104 | + ); |
| 105 | + selections.add( joinEntityA.get( "id" ) ); |
| 106 | + selections.add( joinEntityA.get( "valueA" ) ); |
| 107 | + |
| 108 | + final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList( |
| 109 | + "entities", |
| 110 | + JoinType.LEFT |
| 111 | + ); |
| 112 | + entitiesB.on( cb.equal( entitiesB.get( "entityType" ), EntityB.class.getName() ) ); |
| 113 | + final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat( |
| 114 | + entitiesB, |
| 115 | + EntityB.class |
| 116 | + ); |
| 117 | + selections.add( joinEntityB.get( "id" ) ); |
| 118 | + selections.add( joinEntityB.get( "valueB" ) ); |
| 119 | + |
| 120 | + query.multiselect( selections ); |
| 121 | + |
| 122 | + final List<Tuple> resultList = em.createQuery( query ).getResultList(); |
| 123 | + assertThat( resultList.size(), is( 10 ) ); |
| 124 | + } |
| 125 | + finally { |
| 126 | + em.close(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @MappedSuperclass |
| 131 | + public static abstract class MyEntity { |
| 132 | + @Id |
| 133 | + @GeneratedValue |
| 134 | + private long id; |
| 135 | + } |
| 136 | + |
| 137 | + @Entity(name = "TestEntity") |
| 138 | + public static class TestEntity extends MyEntity { |
| 139 | + @OneToMany(mappedBy = "parent", cascade = {CascadeType.ALL}) |
| 140 | + private List<AbstractEntity> entities = new ArrayList<>(); |
| 141 | + } |
| 142 | + |
| 143 | + @Entity(name = "AbstractEntity") |
| 144 | + public static abstract class AbstractEntity extends MyEntity { |
| 145 | + String entityType = getClass().getName(); |
| 146 | + |
| 147 | + @ManyToOne |
| 148 | + private TestEntity parent; |
| 149 | + |
| 150 | + public TestEntity getParent() { |
| 151 | + return parent; |
| 152 | + } |
| 153 | + |
| 154 | + public void setParent(TestEntity parent) { |
| 155 | + this.parent = parent; |
| 156 | + parent.entities.add( this ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Entity(name = "EntityA") |
| 161 | + public static class EntityA extends AbstractEntity { |
| 162 | + public String valueA; |
| 163 | + |
| 164 | + public EntityA() { |
| 165 | + super.entityType = getClass().getName(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Entity(name = "EntityB") |
| 170 | + public static class EntityB extends AbstractEntity { |
| 171 | + public String valueB; |
| 172 | + |
| 173 | + public EntityB() { |
| 174 | + super.entityType = getClass().getName(); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments