Skip to content

Commit

Permalink
HHH-16120 Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 authored and beikov committed Feb 6, 2023
1 parent 5295c5a commit f81426c
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
);
}
}
Original file line number Diff line number Diff line change
@@ -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<Child> 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<Child> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
for ( Iterator<Child> i = children.iterator(); i.hasNext(); ) {
if ( i.next() == null ) {
i.remove();
}
}
}

public void addChild(Child child) {
this.children.add( child );
child.setParent( this );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false" package="org.hibernate.orm.test.collection.list">
<class name="Parent" table="custom_field">
<id name="id"/>
<property name="name" column="name"/>
<list name="children" inverse="true" cascade="all-delete-orphan">
<key column="custom_field_id"/>
<index column="seq_num"/>
<one-to-many class="Child"/>
</list>
</class>
<class name="Child" table="custom_field_option">
<id name="id"/>
<many-to-one name="parent" not-null="true" column="custom_field_id"/>
</class>
</hibernate-mapping>

0 comments on commit f81426c

Please sign in to comment.