Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-07-13 13:42:45 +02:00
parent 3022371d3e
commit a648e637cc
10 changed files with 366 additions and 318 deletions

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.inheritance.discriminator; package org.hibernate.orm.test.inheritance.discriminator;
import java.sql.Statement; import java.sql.Statement;
import java.util.Map; import java.util.Map;
@ -17,27 +17,37 @@ import javax.persistence.Inheritance;
import javax.persistence.InheritanceType; import javax.persistence.InheritanceType;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.Test; 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; import static org.assertj.core.api.Assertions.assertThat;
@TestForIssue(jiraKey = "HHH-12445") @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 @AfterEach
protected Class<?>[] getAnnotatedClasses() { public void tearDown(SessionFactoryScope scope) {
return new Class<?>[] { scope.inTransaction(
RootEntity.class, session ->
Val1Entity.class, session.createQuery( "delete from root_ent" ).executeUpdate()
Val2Entity.class, );
NotNullEntity.class
};
} }
@Test @Test
public void test() { public void test(SessionFactoryScope scope) {
inTransaction( session -> { scope.inTransaction( session -> {
Val1Entity val1 = new Val1Entity(); Val1Entity val1 = new Val1Entity();
val1.setId( 1L ); val1.setId( 1L );
@ -61,7 +71,7 @@ public class SingleTableNullNotNullDiscriminatorTest extends BaseCoreFunctionalT
} ); } );
} ); } );
inTransaction( session -> { scope.inTransaction( session -> {
Map<Long, RootEntity> entities = session.createQuery( Map<Long, RootEntity> entities = session.createQuery(
"select e from root_ent e", RootEntity.class ) "select e from root_ent e", RootEntity.class )
.getResultList() .getResultList()
@ -83,6 +93,8 @@ public class SingleTableNullNotNullDiscriminatorTest extends BaseCoreFunctionalT
@Id @Id
private Long id; private Long id;
private String name;
public Long getId() { public Long getId() {
return id; return id;
} }

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.reattachment; package org.hibernate.orm.test.reattachment;
/** /**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.reattachment; package org.hibernate.orm.test.reattachment;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
@ -19,7 +19,7 @@ import org.junit.jupiter.api.Test;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@DomainModel( @DomainModel(
xmlMappings = " org/hibernate/test/reattachment/Mappings.hbm.xml" xmlMappings = "org/hibernate/orm/test/reattachment/Mappings.hbm.xml"
) )
@SessionFactory @SessionFactory
public class CollectionReattachmentTest { public class CollectionReattachmentTest {

View File

@ -10,7 +10,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.reattachment"> <hibernate-mapping package="org.hibernate.orm.test.reattachment">
<class name="Parent"> <class name="Parent">
<id name="name" column="NAME" type="string" /> <id name="name" column="NAME" type="string" />

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.reattachment; package org.hibernate.orm.test.reattachment;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Parent> 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<Parent> 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<Parent> 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<Parent> 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 );
}
);
}
}

View File

@ -46,7 +46,7 @@ public class StatelessSessionQueryTest extends BaseCoreFunctionalTestCase {
TestData testData=new TestData(); TestData testData=new TestData();
testData.createData(); testData.createData();
StatelessSession s = sessionFactory().openStatelessSession(); 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() ); .list().size() );
s.close(); s.close();
testData.cleanData(); testData.cleanData();
@ -59,7 +59,7 @@ public class StatelessSessionQueryTest extends BaseCoreFunctionalTestCase {
TestData testData=new TestData(); TestData testData=new TestData();
testData.createData(); 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(); StatelessSession s = sessionFactory().openStatelessSession();
org.hibernate.query.Query query = s.createQuery( queryString ); org.hibernate.query.Query query = s.createQuery( queryString );

View File

@ -26,6 +26,7 @@ import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
@ -87,7 +88,7 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase {
StatelessSession ss = sessionFactory().openStatelessSession(); StatelessSession ss = sessionFactory().openStatelessSession();
ss.beginTransaction(); ss.beginTransaction();
Task taskRef = (Task) ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ).uniqueResult(); Task taskRef = (Task) ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ).uniqueResult();
assertTrue( taskRef != null ); assertNotNull( taskRef );
assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef ) );
assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
@ -133,7 +134,7 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase {
final Query query = ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ); 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 ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY );
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Task taskRef = (Task) ( (Object[]) scrollableResults.get() )[0]; Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef ) );
assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
@ -180,7 +181,7 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase {
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 ); final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY );
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Task taskRef = (Task) ( (Object[]) scrollableResults.get() )[0]; Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef ) );
assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
@ -225,20 +226,10 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase {
ss.beginTransaction(); ss.beginTransaction();
final Query query = ss.createQuery( "select p from Producer p join fetch p.products" ); final Query query = ss.createQuery( "select p from Producer p join fetch p.products" );
ScrollableResults scrollableResults = null; final ScrollableResults scrollableResults = getScrollableResults( query );
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 );
}
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Producer producer = (Producer) ( (Object[]) scrollableResults.get() )[0]; Producer producer = (Producer) scrollableResults.get();
assertTrue( Hibernate.isInitialized( producer ) ); assertTrue( Hibernate.isInitialized( producer ) );
assertTrue( Hibernate.isInitialized( producer.getProducts() ) ); assertTrue( Hibernate.isInitialized( producer.getProducts() ) );
@ -254,6 +245,21 @@ public class StatelessSessionFetchingTest extends BaseCoreFunctionalTestCase {
cleanup(); 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() { private void cleanup() {
Session s = openSession(); Session s = openSession();
s.beginTransaction(); s.beginTransaction();

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}