Skip to content

Commit f81426c

Browse files
dreab8beikov
authored andcommitted
HHH-16120 Add test for issue
1 parent 5295c5a commit f81426c

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.hibernate.orm.test.collection.list;
2+
3+
import jakarta.persistence.Id;
4+
import jakarta.persistence.ManyToOne;
5+
6+
public class Child {
7+
private Integer id;
8+
private String name;
9+
10+
11+
private Parent parent;
12+
13+
public Child() {
14+
}
15+
16+
public Child(Integer id, String name) {
17+
this.id = id;
18+
this.name = name;
19+
}
20+
21+
@Id
22+
public Integer getId() {
23+
return id;
24+
}
25+
26+
public void setId(Integer id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
@ManyToOne
39+
public Parent getParent() {
40+
return parent;
41+
}
42+
43+
public void setParent(Parent parent) {
44+
this.parent = parent;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hibernate.orm.test.collection.list;
2+
3+
import org.hibernate.testing.orm.junit.DomainModel;
4+
import org.hibernate.testing.orm.junit.SessionFactory;
5+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
@DomainModel(
10+
xmlMappings = "org/hibernate/orm/test/collection/list/ParentChildMapping.hbm.xml"
11+
)
12+
@SessionFactory
13+
public class IterateOverListInTheSetMethodTest {
14+
15+
@BeforeAll
16+
public void setUp(SessionFactoryScope scope) {
17+
scope.inTransaction(
18+
session -> {
19+
Child child = new Child( 1, "Luigi" );
20+
Child child2 = new Child( 2, "Franco" );
21+
Parent parent = new Parent( 2, "Fabio" );
22+
parent.addChild( child );
23+
parent.addChild( child2 );
24+
25+
session.persist( parent );
26+
session.persist( child );
27+
session.persist( child2 );
28+
}
29+
);
30+
}
31+
32+
@Test
33+
public void testHqlQuery(SessionFactoryScope scope) {
34+
scope.inSession(
35+
session -> {
36+
session.createQuery( "select p from Parent p" ).list();
37+
}
38+
);
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.hibernate.orm.test.collection.list;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.OneToMany;
11+
12+
public class Parent {
13+
14+
private Integer id;
15+
private String name;
16+
17+
18+
private List<Child> children = new ArrayList<>();
19+
20+
public Parent() {
21+
}
22+
23+
public Parent(Integer id, String name) {
24+
this.id = id;
25+
this.name = name;
26+
}
27+
28+
@Id
29+
public Integer getId() {
30+
return id;
31+
}
32+
33+
public void setId(Integer id) {
34+
this.id = id;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
public List<Child> getChildren() {
46+
return children;
47+
}
48+
49+
public void setChildren(List<Child> children) {
50+
this.children = children;
51+
for ( Iterator<Child> i = children.iterator(); i.hasNext(); ) {
52+
if ( i.next() == null ) {
53+
i.remove();
54+
}
55+
}
56+
}
57+
58+
public void addChild(Child child) {
59+
this.children.add( child );
60+
child.setParent( this );
61+
}
62+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
4+
5+
<hibernate-mapping default-lazy="false" package="org.hibernate.orm.test.collection.list">
6+
<class name="Parent" table="custom_field">
7+
<id name="id"/>
8+
<property name="name" column="name"/>
9+
<list name="children" inverse="true" cascade="all-delete-orphan">
10+
<key column="custom_field_id"/>
11+
<index column="seq_num"/>
12+
<one-to-many class="Child"/>
13+
</list>
14+
</class>
15+
<class name="Child" table="custom_field_option">
16+
<id name="id"/>
17+
<many-to-one name="parent" not-null="true" column="custom_field_id"/>
18+
</class>
19+
</hibernate-mapping>

0 commit comments

Comments
 (0)