From a648e637cc235815dd146e56bbfe68cf31225e85 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 13 Jul 2021 13:42:45 +0200 Subject: [PATCH] Re-enabled additional tests --- ...ngleTableNullNotNullDiscriminatorTest.java | 42 ++- .../{ => orm}/test/reattachment/Child.java | 2 +- .../CollectionReattachmentTest.java | 4 +- .../test/reattachment/Mappings.hbm.xml | 2 +- .../{ => orm}/test/reattachment/Parent.java | 2 +- .../reattachment/ProxyReattachmentTest.java | 109 +++++++ .../reattachment/ProxyReattachmentTest.java | 270 ------------------ .../stateless/StatelessSessionQueryTest.java | 4 +- .../StatelessSessionFetchingTest.java | 58 ++-- .../reattachment/ProxyReattachmentTest.java | 191 +++++++++++++ 10 files changed, 366 insertions(+), 318 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java (70%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/reattachment/Child.java (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/reattachment/CollectionReattachmentTest.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/reattachment/Mappings.hbm.xml (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/reattachment/Parent.java (94%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/ProxyReattachmentTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java create mode 100644 hibernate-core/src/test_legacy/org/hibernate/test/reattachment/ProxyReattachmentTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java similarity index 70% rename from hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java index 71d48e40c6..6b4950d408 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableNullNotNullDiscriminatorTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.inheritance.discriminator; +package org.hibernate.orm.test.inheritance.discriminator; import java.sql.Statement; import java.util.Map; @@ -17,27 +17,37 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @TestForIssue(jiraKey = "HHH-12445") -public class SingleTableNullNotNullDiscriminatorTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + SingleTableNullNotNullDiscriminatorTest.RootEntity.class, + SingleTableNullNotNullDiscriminatorTest.Val1Entity.class, + SingleTableNullNotNullDiscriminatorTest.Val2Entity.class, + SingleTableNullNotNullDiscriminatorTest.NotNullEntity.class + } +) +@SessionFactory +public class SingleTableNullNotNullDiscriminatorTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - RootEntity.class, - Val1Entity.class, - Val2Entity.class, - NotNullEntity.class - }; + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from root_ent" ).executeUpdate() + ); } @Test - public void test() { - inTransaction( session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Val1Entity val1 = new Val1Entity(); val1.setId( 1L ); @@ -61,7 +71,7 @@ public class SingleTableNullNotNullDiscriminatorTest extends BaseCoreFunctionalT } ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { Map entities = session.createQuery( "select e from root_ent e", RootEntity.class ) .getResultList() @@ -83,6 +93,8 @@ public class SingleTableNullNotNullDiscriminatorTest extends BaseCoreFunctionalT @Id private Long id; + private String name; + public Long getId() { return id; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Child.java b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Child.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/reattachment/Child.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Child.java index 280ff013cd..008616b01a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Child.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Child.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.reattachment; +package org.hibernate.orm.test.reattachment; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/CollectionReattachmentTest.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/CollectionReattachmentTest.java index 32e92195d3..97b4d30d95 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/CollectionReattachmentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/CollectionReattachmentTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.reattachment; +package org.hibernate.orm.test.reattachment; import org.hibernate.testing.orm.junit.DomainModel; @@ -19,7 +19,7 @@ import org.junit.jupiter.api.Test; * @author Steve Ebersole */ @DomainModel( - xmlMappings = " org/hibernate/test/reattachment/Mappings.hbm.xml" + xmlMappings = "org/hibernate/orm/test/reattachment/Mappings.hbm.xml" ) @SessionFactory public class CollectionReattachmentTest { diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Mappings.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Mappings.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/reattachment/Mappings.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Mappings.hbm.xml index 86aee42aa5..a1c0bbe983 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Mappings.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Mappings.hbm.xml @@ -10,7 +10,7 @@ "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Parent.java b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Parent.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/reattachment/Parent.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Parent.java index e0015f9955..75b808ebc0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/Parent.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/Parent.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.reattachment; +package org.hibernate.orm.test.reattachment; import java.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/ProxyReattachmentTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/ProxyReattachmentTest.java new file mode 100644 index 0000000000..13777a6485 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/reattachment/ProxyReattachmentTest.java @@ -0,0 +1,109 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.reattachment; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +/** + * Test of proxy reattachment semantics + * + * @author Steve Ebersole + */ +@DomainModel( + xmlMappings = "org/hibernate/orm/test/reattachment/Mappings.hbm.xml" +) +@SessionFactory +public class ProxyReattachmentTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from Parent" ).executeUpdate(); + session.createQuery( "delete from Child" ).executeUpdate(); + } + ); + } + + @Test + public void testUpdateAfterEvict(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Parent p = new Parent( "p" ); + session.save( p ); + } + ); + + Parent parent = scope.fromTransaction( + session -> { + Parent p = session.load( Parent.class, "p" ); + // evict... + session.evict( p ); + // now try to reattach... + session.update( p ); + return p; + } + ); + + scope.inTransaction( + session -> + session.delete( parent ) + ); + } + + @Test + public void testUpdateAfterClear(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Parent p = new Parent( "p" ); + session.save( p ); + } + ); + + Parent parent = scope.fromTransaction( + session -> { + Parent p = session.load( Parent.class, "p" ); + // clear... + session.clear(); + // now try to reattach... + session.update( p ); + return p; + } + ); + + scope.inTransaction( + session -> + session.delete( parent ) + ); + } + + + @Test + @TestForIssue(jiraKey = "HHH-8374") + public void testRemoveAndReattachProxyEntity(SessionFactoryScope scope) { + Parent p = new Parent( "foo" ); + scope.inTransaction( + session -> { + session.persist( p ); + } + ); + + scope.inTransaction( + session -> { + Parent parent = session.load( Parent.class, p.getName() ); + session.delete( parent ); + // re-attach + session.persist( parent ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java b/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java deleted file mode 100644 index 319910508b..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.reattachment; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Test of proxy reattachment semantics - * - * @author Steve Ebersole - */ -@DomainModel( - xmlMappings = "org/hibernate/test/reattachment/Mappings.hbm.xml" -) -@SessionFactory -public class ProxyReattachmentTest { - - @AfterEach - public void tearDown(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - session.createQuery( "delete from Parent" ); - session.createQuery( "delete from Child" ); - } - ); - } - - @Test - public void testUpdateAfterEvict(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - Parent p = new Parent( "p" ); - session.save( p ); - } - ); - - Parent parent = scope.fromTransaction( - session -> { - Parent p = session.load( Parent.class, "p" ); - // evict... - session.evict( p ); - // now try to reattach... - session.update( p ); - return p; - } - ); - - scope.inTransaction( - session -> - session.delete( parent ) - ); - } - - @Test - public void testUpdateAfterClear(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - Parent p = new Parent( "p" ); - session.save( p ); - } - ); - - Parent parent = scope.fromTransaction( - session -> { - Parent p = session.load( Parent.class, "p" ); - // clear... - session.clear(); - // now try to reattach... - session.update( p ); - return p; - } - ); - - scope.inTransaction( - session -> - session.delete( parent ) - ); - } - - @Test - @SuppressWarnings({ "unchecked" }) - public void testIterateWithClearTopOfLoop(SessionFactoryScope scope) { - Set parents = new HashSet(); - scope.inTransaction( - session -> { - for ( int i = 0; i < 5; i++ ) { - Parent p = new Parent( String.valueOf( i ) ); - Child child = new Child( "child" + i ); - child.setParent( p ); - p.getChildren().add( child ); - session.save( p ); - parents.add( p ); - } - } - ); - - scope.inTransaction( - session -> { - int i = 0; - List fromParent = session.createQuery( "from Parent" ).list(); - for ( Parent p : fromParent ) { - i++; - if ( i % 2 == 0 ) { - session.flush(); - session.clear(); - } - assertEquals( 1, p.getChildren().size() ); - } - } - ); - - scope.inTransaction( - session -> { - for ( Object parent : parents ) { - session.delete( parent ); - } - } - ); - } - - @Test - @SuppressWarnings({ "unchecked" }) - public void testIterateWithClearBottomOfLoop(SessionFactoryScope scope) { - Set parents = new HashSet(); - scope.inTransaction( - session -> { - for ( int i = 0; i < 5; i++ ) { - Parent p = new Parent( String.valueOf( i ) ); - Child child = new Child( "child" + i ); - child.setParent( p ); - p.getChildren().add( child ); - session.save( p ); - parents.add( p ); - } - } - ); - - scope.inTransaction( - session -> { - int i = 0; - List fromParent = session.createQuery( "from Parent" ).list(); - for ( Parent p : fromParent ) { - assertEquals( 1, p.getChildren().size() ); - i++; - if ( i % 2 == 0 ) { - session.flush(); - session.clear(); - } - } - } - ); - - scope.inTransaction( - session -> { - for ( Object parent : parents ) { - session.delete( parent ); - } - } - ); - } - - @Test - @SuppressWarnings({ "unchecked" }) - public void testIterateWithEvictTopOfLoop(SessionFactoryScope scope) { - Set parents = new HashSet(); - scope.inTransaction( - session -> { - for ( int i = 0; i < 5; i++ ) { - Parent p = new Parent( String.valueOf( i + 100 ) ); - Child child = new Child( "child" + i ); - child.setParent( p ); - p.getChildren().add( child ); - session.save( p ); - parents.add( p ); - } - } - ); - - scope.inTransaction( - session -> { - List fromParent = session.createQuery( "from Parent" ).list(); - for ( Parent p : fromParent ) { - if ( p != null ) { - session.evict( p ); - } - assertEquals( 1, p.getChildren().size() ); - } - } - ); - - scope.inTransaction( - session -> { - for ( Object parent : parents ) { - session.delete( parent ); - } - } - ); - } - - @Test - @SuppressWarnings({ "unchecked" }) - public void testIterateWithEvictBottomOfLoop(SessionFactoryScope scope) { - Set parents = new HashSet(); - scope.inTransaction( - session -> { - for ( int i = 0; i < 5; i++ ) { - Parent p = new Parent( String.valueOf( i + 100 ) ); - Child child = new Child( "child" + i ); - child.setParent( p ); - p.getChildren().add( child ); - session.save( p ); - parents.add( p ); - } - } - ); - - scope.inTransaction( - session -> { - List fromParent = session.createQuery( "from Parent" ).list(); - for ( Parent p : fromParent ) { - assertEquals( 1, p.getChildren().size() ); - session.evict( p ); - } - } - ); - - scope.inTransaction( - session -> { - for ( Object parent : parents ) { - session.delete( parent ); - } - } - ); - } - - @Test - @TestForIssue(jiraKey = "HHH-8374") - public void testRemoveAndReattachProxyEntity(SessionFactoryScope scope) { - Parent p = new Parent( "foo" ); - scope.inTransaction( - session -> { - session.persist( p ); - } - ); - - scope.inTransaction( - session -> { - Parent parent = session.load( Parent.class, p.getName() ); - session.delete( parent ); - // re-attach - session.persist( parent ); - } - ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java index 9677f2ffa7..a1143392db 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java @@ -46,7 +46,7 @@ public class StatelessSessionQueryTest extends BaseCoreFunctionalTestCase { TestData testData=new TestData(); testData.createData(); StatelessSession s = sessionFactory().openStatelessSession(); - assertEquals( 1, s.createQuery( "from Contact c join fetch c.org join fetch c.org.country" ) + assertEquals( 1, s.createQuery( "from Contact c join fetch c.org o join fetch c.org.country" ) .list().size() ); s.close(); testData.cleanData(); @@ -59,7 +59,7 @@ public class StatelessSessionQueryTest extends BaseCoreFunctionalTestCase { TestData testData=new TestData(); testData.createData(); - final String queryString = "from Contact c join fetch c.org join fetch c.org.country"; + final String queryString = "from Contact c join fetch c.org o join fetch o.country"; StatelessSession s = sessionFactory().openStatelessSession(); org.hibernate.query.Query query = s.createQuery( queryString ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/fetching/StatelessSessionFetchingTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/fetching/StatelessSessionFetchingTest.java index 3b9547d8b1..ff7793c4a2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stateless/fetching/StatelessSessionFetchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/fetching/StatelessSessionFetchingTest.java @@ -26,6 +26,7 @@ import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; /** @@ -54,7 +55,7 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { protected String applyPrefix(String baseTableName) { String prefixed = prefix + '_' + baseTableName; - log.debug("prefixed table name : " + baseTableName + " -> " + prefixed); + log.debug( "prefixed table name : " + baseTableName + " -> " + prefixed ); return prefixed; } @@ -64,7 +65,7 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { } private String determineUniquePrefix() { - return StringHelper.collapseQualifier( getClass().getName(), false ).toUpperCase(Locale.ROOT); + return StringHelper.collapseQualifier( getClass().getName(), false ).toUpperCase( Locale.ROOT ); } } @@ -86,8 +87,8 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { StatelessSession ss = sessionFactory().openStatelessSession(); ss.beginTransaction(); - Task taskRef = ( Task ) ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ).uniqueResult(); - assertTrue( taskRef != null ); + Task taskRef = (Task) ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ).uniqueResult(); + assertNotNull( taskRef ); assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); @@ -130,10 +131,10 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { StatelessSession ss = sessionFactory().openStatelessSession(); ss.beginTransaction(); - final Query query = ss.createQuery( "from Task t join fetch t.resource join fetch t.user"); - final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY); + final Query query = ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ); + final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); while ( scrollableResults.next() ) { - Task taskRef = (Task) ( (Object[]) scrollableResults.get() )[0]; + Task taskRef = (Task) scrollableResults.get(); assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); @@ -177,10 +178,10 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { inTransaction( session -> { - final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user"); + final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" ); final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); while ( scrollableResults.next() ) { - Task taskRef = (Task) ( (Object[]) scrollableResults.get() )[0]; + Task taskRef = (Task) scrollableResults.get(); assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); @@ -210,9 +211,9 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { session.save( v1 ); session.save( v2 ); - final Product product1 = new Product(1, "123", v1, p1); - final Product product2 = new Product(2, "456", v1, p1); - final Product product3 = new Product(3, "789", v1, p2); + final Product product1 = new Product( 1, "123", v1, p1 ); + final Product product2 = new Product( 2, "456", v1, p1 ); + final Product product3 = new Product( 3, "789", v1, p2 ); session.save( product1 ); session.save( product2 ); @@ -225,24 +226,14 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { ss.beginTransaction(); final Query query = ss.createQuery( "select p from Producer p join fetch p.products" ); - ScrollableResults scrollableResults = null; - if ( getDialect() instanceof DB2Dialect ) { - /* - FetchingScrollableResultsImp#next() in order to check if the ResultSet is empty calls ResultSet#isBeforeFirst() - but the support for ResultSet#isBeforeFirst() is optional for ResultSets with a result - set type of TYPE_FORWARD_ONLY and db2 does not support it. - */ - scrollableResults = query.scroll( ScrollMode.SCROLL_INSENSITIVE ); - } - else { - scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); - } + final ScrollableResults scrollableResults = getScrollableResults( query ); + while ( scrollableResults.next() ) { - Producer producer = (Producer) ( (Object[]) scrollableResults.get() )[0]; + Producer producer = (Producer) scrollableResults.get(); assertTrue( Hibernate.isInitialized( producer ) ); assertTrue( Hibernate.isInitialized( producer.getProducts() ) ); - for (Product product : producer.getProducts()) { + for ( Product product : producer.getProducts() ) { assertTrue( Hibernate.isInitialized( product ) ); assertFalse( Hibernate.isInitialized( product.getVendor() ) ); } @@ -254,6 +245,21 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase { cleanup(); } + private ScrollableResults getScrollableResults(Query query) { + ScrollableResults scrollableResults; + if ( getDialect() instanceof DB2Dialect ) { + /* + FetchingScrollableResultsImp#next() in order to check if the ResultSet is empty calls ResultSet#isBeforeFirst() + but the support for ResultSet#isBeforeFirst() is optional for ResultSets with a result + set type of TYPE_FORWARD_ONLY and db2 does not support it. + */ + return query.scroll( ScrollMode.SCROLL_INSENSITIVE ); + } + else { + return query.scroll( ScrollMode.FORWARD_ONLY ); + } + } + private void cleanup() { Session s = openSession(); s.beginTransaction(); diff --git a/hibernate-core/src/test_legacy/org/hibernate/test/reattachment/ProxyReattachmentTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/reattachment/ProxyReattachmentTest.java new file mode 100644 index 0000000000..bce202d703 --- /dev/null +++ b/hibernate-core/src/test_legacy/org/hibernate/test/reattachment/ProxyReattachmentTest.java @@ -0,0 +1,191 @@ +/* + * 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 . + */ +package org.hibernate.test.reattachment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.proxy.HibernateProxy; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +/** + * Test of proxy reattachment semantics + * + * @author Steve Ebersole + */ +public class ProxyReattachmentTest extends BaseCoreFunctionalTestCase { + public String[] getMappings() { + return new String[] { "reattachment/Mappings.hbm.xml" }; + } + + @SuppressWarnings( {"unchecked"}) + public void testIterateWithClearTopOfLoop() { + Session s = openSession(); + s.beginTransaction(); + Set parents = new HashSet(); + for (int i=0; i<5; i++) { + Parent p = new Parent( String.valueOf( i ) ); + Child child = new Child( "child" + i ); + child.setParent( p ); + p.getChildren().add( child ); + s.save( p ); + parents.add(p); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + int i = 0; + for ( Iterator it = session.createQuery( "from Parent p " ).iterate(); it.hasNext(); ) { + i++; + if (i % 2 == 0) { + s.flush(); + s.clear(); + } + Parent p = (Parent) it.next(); + + assertEquals( 1, p.getChildren().size() ); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + for ( Object parent : parents ) { + s.delete( parent ); + } + s.getTransaction().commit(); + s.close(); + } + + @Test + @SuppressWarnings( {"unchecked"}) + public void testIterateWithClearBottomOfLoop() { + Session s = openSession(); + s.beginTransaction(); + Set parents = new HashSet(); + for (int i=0; i<5; i++) { + Parent p = new Parent( String.valueOf( i ) ); + Child child = new Child( "child" + i ); + child.setParent( p ); + p.getChildren().add( child ); + s.save( p ); + parents.add(p); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + int i = 0; + for (Iterator it = session.createQuery( "from Parent p " ).iterate(); it.hasNext(); ) { + Parent p = (Parent) it.next(); + assertEquals( 1, p.getChildren().size() ); + i++; + if (i % 2 == 0) { + s.flush(); + s.clear(); + } + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + for ( Object parent : parents ) { + s.delete( parent ); + } + s.getTransaction().commit(); + s.close(); + } + + @Test + @SuppressWarnings( {"unchecked"}) + public void testIterateWithEvictTopOfLoop() { + Session s = openSession(); + s.beginTransaction(); + Set parents = new HashSet(); + for (int i=0; i<5; i++) { + Parent p = new Parent( String.valueOf( i + 100 ) ); + Child child = new Child( "child" + i ); + child.setParent( p ); + p.getChildren().add( child ); + s.save( p ); + parents.add(p); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + Parent p = null; + for (Iterator it = session.createQuery( "from Parent" ).iterate(); it.hasNext(); ) { + if ( p != null) { s.evict(p); } + p = (Parent) it.next(); + assertEquals( 1, p.getChildren().size() ); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + for ( Object parent : parents ) { + s.delete( parent ); + } + s.getTransaction().commit(); + s.close(); + } + + @Test + @SuppressWarnings( {"unchecked"}) + public void testIterateWithEvictBottomOfLoop() { + Session s = openSession(); + s.beginTransaction(); + Set parents = new HashSet(); + for (int i=0; i<5; i++) { + Parent p = new Parent( String.valueOf( i + 100 ) ); + Child child = new Child( "child" + i ); + child.setParent( p ); + p.getChildren().add( child ); + s.save( p ); + parents.add(p); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + for (Iterator it = s.createQuery( "from Parent" ).iterate(); it.hasNext(); ) { + Parent p = (Parent) it.next(); + assertEquals( 1, p.getChildren().size() ); + s.evict(p); + } + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + for ( Object parent : parents ) { + s.delete( parent ); + } + s.getTransaction().commit(); + s.close(); + } + +}