|
| 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.test.bytecode.enhancement.lazy.proxy; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.LinkedHashSet; |
| 11 | +import java.util.Set; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.FetchType; |
| 14 | +import javax.persistence.Id; |
| 15 | +import javax.persistence.Inheritance; |
| 16 | +import javax.persistence.InheritanceType; |
| 17 | +import javax.persistence.JoinColumn; |
| 18 | +import javax.persistence.ManyToOne; |
| 19 | +import javax.persistence.OneToMany; |
| 20 | +import javax.persistence.Table; |
| 21 | + |
| 22 | +import org.hibernate.Hibernate; |
| 23 | +import org.hibernate.ScrollMode; |
| 24 | +import org.hibernate.ScrollableResults; |
| 25 | +import org.hibernate.Session; |
| 26 | +import org.hibernate.StatelessSession; |
| 27 | +import org.hibernate.annotations.LazyToOne; |
| 28 | +import org.hibernate.annotations.LazyToOneOption; |
| 29 | +import org.hibernate.boot.MetadataSources; |
| 30 | +import org.hibernate.boot.SessionFactoryBuilder; |
| 31 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 32 | +import org.hibernate.cfg.AvailableSettings; |
| 33 | +import org.hibernate.dialect.DB2Dialect; |
| 34 | +import org.hibernate.query.Query; |
| 35 | +import org.hibernate.stat.spi.StatisticsImplementor; |
| 36 | + |
| 37 | +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; |
| 38 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 39 | +import org.junit.After; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | + |
| 44 | +import static org.hamcrest.CoreMatchers.is; |
| 45 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Andrea Boriero |
| 49 | + */ |
| 50 | +@RunWith(BytecodeEnhancerRunner.class) |
| 51 | +public class QueryScrollingWithInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCase { |
| 52 | + @Override |
| 53 | + protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { |
| 54 | + super.configureStandardServiceRegistryBuilder( ssrb ); |
| 55 | + ssrb.applySetting( AvailableSettings.ALLOW_ENHANCEMENT_AS_PROXY, "true" ); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { |
| 60 | + super.configureSessionFactoryBuilder( sfb ); |
| 61 | + sfb.applyStatisticsSupport( true ); |
| 62 | + sfb.applySecondLevelCacheSupport( false ); |
| 63 | + sfb.applyQueryCacheSupport( false ); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void applyMetadataSources(MetadataSources sources) { |
| 68 | + super.applyMetadataSources( sources ); |
| 69 | + sources.addAnnotatedClass( EmployeeParent.class ); |
| 70 | + sources.addAnnotatedClass( Employee.class ); |
| 71 | + sources.addAnnotatedClass( OtherEntity.class ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testScrollableWithStatelessSession() { |
| 76 | + final StatisticsImplementor stats = sessionFactory().getStatistics(); |
| 77 | + stats.clear(); |
| 78 | + ScrollableResults scrollableResults = null; |
| 79 | + final StatelessSession statelessSession = sessionFactory().openStatelessSession(); |
| 80 | + |
| 81 | + try { |
| 82 | + statelessSession.beginTransaction(); |
| 83 | + Query<Employee> query = statelessSession.createQuery( |
| 84 | + "select distinct e from Employee e left join fetch e.otherEntities", |
| 85 | + Employee.class |
| 86 | + ); |
| 87 | + if ( getDialect() instanceof DB2Dialect ) { |
| 88 | + /* |
| 89 | + FetchingScrollableResultsImp#next() in order to check if the ResultSet is empty calls ResultSet#isBeforeFirst() |
| 90 | + but the support for ResultSet#isBeforeFirst() is optional for ResultSets with a result |
| 91 | + set type of TYPE_FORWARD_ONLY and db2 does not support it. |
| 92 | + */ |
| 93 | + scrollableResults = query.scroll( ScrollMode.SCROLL_INSENSITIVE ); |
| 94 | + } |
| 95 | + else { |
| 96 | + scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); |
| 97 | + } |
| 98 | + |
| 99 | + while ( scrollableResults.next() ) { |
| 100 | + final Employee employee = (Employee) scrollableResults.get( 0 ); |
| 101 | + assertThat( Hibernate.isPropertyInitialized( employee, "otherEntities" ), is( true ) ); |
| 102 | + assertThat( Hibernate.isInitialized( employee.getOtherEntities() ), is( true ) ); |
| 103 | + if ( "ENG1".equals( employee.getDept() ) ) { |
| 104 | + assertThat( employee.getOtherEntities().size(), is( 2 ) ); |
| 105 | + for ( OtherEntity otherEntity : employee.getOtherEntities() ) { |
| 106 | + if ( "test1".equals( otherEntity.id ) ) { |
| 107 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employee" ), is( true ) ); |
| 108 | + assertThat( otherEntity.employee, is( employee ) ); |
| 109 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employeeParent" ), is( true ) ); |
| 110 | + assertThat( otherEntity.employeeParent, is( employee ) ); |
| 111 | + } |
| 112 | + else { |
| 113 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employee" ), is( true ) ); |
| 114 | + assertThat( otherEntity.employee, is( employee ) ); |
| 115 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employeeParent" ), is( true ) ); |
| 116 | + assertThat( Hibernate.isInitialized( otherEntity.employeeParent ), is( false ) ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + else { |
| 121 | + assertThat( employee.getOtherEntities().size(), is( 0 ) ); |
| 122 | + } |
| 123 | + } |
| 124 | + statelessSession.getTransaction().commit(); |
| 125 | + assertThat( stats.getPrepareStatementCount(), is( 1L ) ); |
| 126 | + } |
| 127 | + finally { |
| 128 | + if ( scrollableResults != null ) { |
| 129 | + scrollableResults.close(); |
| 130 | + } |
| 131 | + if ( statelessSession.getTransaction().isActive() ) { |
| 132 | + statelessSession.getTransaction().rollback(); |
| 133 | + } |
| 134 | + statelessSession.close(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testScrollableWithSession() { |
| 140 | + final StatisticsImplementor stats = sessionFactory().getStatistics(); |
| 141 | + stats.clear(); |
| 142 | + ScrollableResults scrollableResults = null; |
| 143 | + final Session session = sessionFactory().openSession(); |
| 144 | + |
| 145 | + try { |
| 146 | + session.beginTransaction(); |
| 147 | + Query<Employee> query = session.createQuery( |
| 148 | + "select distinct e from Employee e left join fetch e.otherEntities", |
| 149 | + Employee.class |
| 150 | + ); |
| 151 | + if ( getDialect() instanceof DB2Dialect ) { |
| 152 | + /* |
| 153 | + FetchingScrollableResultsImp#next() in order to check if the ResultSet is empty calls ResultSet#isBeforeFirst() |
| 154 | + but the support for ResultSet#isBeforeFirst() is optional for ResultSets with a result |
| 155 | + set type of TYPE_FORWARD_ONLY and db2 does not support it. |
| 156 | + */ |
| 157 | + scrollableResults = query.scroll( ScrollMode.SCROLL_INSENSITIVE ); |
| 158 | + } |
| 159 | + else { |
| 160 | + scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); |
| 161 | + } |
| 162 | + |
| 163 | + while ( scrollableResults.next() ) { |
| 164 | + final Employee employee = (Employee) scrollableResults.get( 0 ); |
| 165 | + assertThat( Hibernate.isPropertyInitialized( employee, "otherEntities" ), is( true ) ); |
| 166 | + assertThat( Hibernate.isInitialized( employee.getOtherEntities() ), is( true ) ); |
| 167 | + if ( "ENG1".equals( employee.getDept() ) ) { |
| 168 | + assertThat( employee.getOtherEntities().size(), is( 2 ) ); |
| 169 | + for ( OtherEntity otherEntity : employee.getOtherEntities() ) { |
| 170 | + if ( "test1".equals( otherEntity.id ) ) { |
| 171 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employee" ), is( true ) ); |
| 172 | + assertThat( otherEntity.employee, is( employee ) ); |
| 173 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employeeParent" ), is( true ) ); |
| 174 | + assertThat( otherEntity.employeeParent, is( employee ) ); |
| 175 | + } |
| 176 | + else { |
| 177 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employee" ), is( true ) ); |
| 178 | + assertThat( otherEntity.employee, is( employee ) ); |
| 179 | + assertThat( Hibernate.isPropertyInitialized( otherEntity, "employeeParent" ), is( true ) ); |
| 180 | + assertThat( Hibernate.isInitialized( otherEntity.employeeParent ), is( false ) ); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + else { |
| 185 | + assertThat( employee.getOtherEntities().size(), is( 0 ) ); |
| 186 | + } |
| 187 | + } |
| 188 | + session.getTransaction().commit(); |
| 189 | + assertThat( stats.getPrepareStatementCount(), is( 1L ) ); |
| 190 | + } |
| 191 | + finally { |
| 192 | + if ( scrollableResults != null ) { |
| 193 | + scrollableResults.close(); |
| 194 | + } |
| 195 | + if ( session.getTransaction().isActive() ) { |
| 196 | + session.getTransaction().rollback(); |
| 197 | + } |
| 198 | + session.close(); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @Before |
| 203 | + public void prepareTestData() { |
| 204 | + inTransaction( |
| 205 | + session -> { |
| 206 | + Employee e1 = new Employee( "ENG1" ); |
| 207 | + Employee e2 = new Employee( "ENG2" ); |
| 208 | + OtherEntity other1 = new OtherEntity( "test1" ); |
| 209 | + OtherEntity other2 = new OtherEntity( "test2" ); |
| 210 | + e1.getOtherEntities().add( other1 ); |
| 211 | + e1.getOtherEntities().add( other2 ); |
| 212 | + e1.getParentOtherEntities().add( other1 ); |
| 213 | + e1.getParentOtherEntities().add( other2 ); |
| 214 | + other1.employee = e1; |
| 215 | + other2.employee = e1; |
| 216 | + other1.employeeParent = e1; |
| 217 | + other2.employeeParent = e2; |
| 218 | + session.persist( other1 ); |
| 219 | + session.persist( other2 ); |
| 220 | + session.persist( e1 ); |
| 221 | + session.persist( e2 ); |
| 222 | + } |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + @After |
| 227 | + public void cleanUpTestData() { |
| 228 | + inTransaction( |
| 229 | + session -> { |
| 230 | + session.createQuery( "delete from OtherEntity" ).executeUpdate(); |
| 231 | + session.createQuery( "delete from Employee" ).executeUpdate(); |
| 232 | + session.createQuery( "delete from EmployeeParent" ).executeUpdate(); |
| 233 | + } |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + @Entity(name = "EmployeeParent") |
| 238 | + @Table(name = "EmployeeParent") |
| 239 | + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 240 | + public static abstract class EmployeeParent { |
| 241 | + |
| 242 | + @Id |
| 243 | + private String dept; |
| 244 | + |
| 245 | + @OneToMany(targetEntity = OtherEntity.class, mappedBy = "employeeParent", fetch = FetchType.LAZY) |
| 246 | + protected Set<OtherEntity> parentOtherEntities = new HashSet<>(); |
| 247 | + |
| 248 | + public Set<OtherEntity> getParentOtherEntities() { |
| 249 | + if ( parentOtherEntities == null ) { |
| 250 | + parentOtherEntities = new LinkedHashSet(); |
| 251 | + } |
| 252 | + return parentOtherEntities; |
| 253 | + } |
| 254 | + |
| 255 | + public void setOtherEntities(Set<OtherEntity> pParentOtherEntites) { |
| 256 | + parentOtherEntities = pParentOtherEntites; |
| 257 | + } |
| 258 | + |
| 259 | + public String getDept() { |
| 260 | + return dept; |
| 261 | + } |
| 262 | + |
| 263 | + protected void setDept(String dept) { |
| 264 | + this.dept = dept; |
| 265 | + } |
| 266 | + |
| 267 | + } |
| 268 | + |
| 269 | + @Entity(name = "Employee") |
| 270 | + @Table(name = "Employee") |
| 271 | + public static class Employee extends EmployeeParent { |
| 272 | + |
| 273 | + @OneToMany(targetEntity = OtherEntity.class, mappedBy = "employee", fetch = FetchType.LAZY) |
| 274 | + protected Set<OtherEntity> otherEntities = new HashSet<>(); |
| 275 | + |
| 276 | + public Employee(String dept) { |
| 277 | + this(); |
| 278 | + setDept( dept ); |
| 279 | + } |
| 280 | + |
| 281 | + protected Employee() { |
| 282 | + // this form used by Hibernate |
| 283 | + } |
| 284 | + |
| 285 | + public Set<OtherEntity> getOtherEntities() { |
| 286 | + if ( otherEntities == null ) { |
| 287 | + otherEntities = new LinkedHashSet(); |
| 288 | + } |
| 289 | + return otherEntities; |
| 290 | + } |
| 291 | + |
| 292 | + public void setOtherEntities(Set<OtherEntity> pOtherEntites) { |
| 293 | + otherEntities = pOtherEntites; |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + @Entity(name = "OtherEntity") |
| 298 | + @Table(name = "OtherEntity") |
| 299 | + public static class OtherEntity { |
| 300 | + |
| 301 | + @Id |
| 302 | + private String id; |
| 303 | + |
| 304 | + @ManyToOne(fetch = FetchType.LAZY) |
| 305 | + @LazyToOne(LazyToOneOption.NO_PROXY) |
| 306 | + @JoinColumn(name = "Employee_Id") |
| 307 | + protected Employee employee = null; |
| 308 | + |
| 309 | + @ManyToOne(fetch = FetchType.LAZY) |
| 310 | + @LazyToOne(LazyToOneOption.NO_PROXY) |
| 311 | + @JoinColumn(name = "EmployeeParent_Id") |
| 312 | + protected EmployeeParent employeeParent = null; |
| 313 | + |
| 314 | + protected OtherEntity() { |
| 315 | + // this form used by Hibernate |
| 316 | + } |
| 317 | + |
| 318 | + public OtherEntity(String id) { |
| 319 | + this.id = id; |
| 320 | + } |
| 321 | + |
| 322 | + public String getId() { |
| 323 | + return id; |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | +} |
0 commit comments