diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Child.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Child.java new file mode 100644 index 0000000000..e5e0e66548 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Child.java @@ -0,0 +1,46 @@ +package org.hibernate.orm.test.collection.list; + +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; + +public class Child { + private Integer id; + private String name; + + + private Parent parent; + + public Child() { + } + + public Child(Integer id, String name) { + this.id = id; + this.name = name; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @ManyToOne + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java new file mode 100644 index 0000000000..3e2c3998a3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/IterateOverListInTheSetMethodTest.java @@ -0,0 +1,40 @@ +package org.hibernate.orm.test.collection.list; + +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.BeforeAll; +import org.junit.jupiter.api.Test; + +@DomainModel( + xmlMappings = "org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml" +) +@SessionFactory +public class IterateOverListInTheSetMethodTest { + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Child child = new Child( 1, "Luigi" ); + Child child2 = new Child( 2, "Franco" ); + Parent parent = new Parent( 2, "Fabio" ); + parent.addChild( child ); + parent.addChild( child2 ); + + session.persist( parent ); + session.persist( child ); + session.persist( child2 ); + } + ); + } + + @Test + public void testHqlQuery(SessionFactoryScope scope) { + scope.inSession( + session -> { + session.createQuery( "select p from Parent p" ).list(); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Parent.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Parent.java new file mode 100644 index 0000000000..f35007589c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/Parent.java @@ -0,0 +1,62 @@ +package org.hibernate.orm.test.collection.list; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; + +public class Parent { + + private Integer id; + private String name; + + + private List children = new ArrayList<>(); + + public Parent() { + } + + public Parent(Integer id, String name) { + this.id = id; + this.name = name; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + for ( Iterator i = children.iterator(); i.hasNext(); ) { + if ( i.next() == null ) { + i.remove(); + } + } + } + + public void addChild(Child child) { + this.children.add( child ); + child.setParent( this ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml new file mode 100644 index 0000000000..77bcc3ad02 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file