-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-14597 Test and fix for NPE while trying to delete cascade to-one …
…association within element collection
- Loading branch information
Showing
7 changed files
with
261 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...rc/test/java/org/hibernate/test/orphan/elementcollection/ElementCollectionOrphanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.hibernate.test.orphan.elementcollection; | ||
|
||
import java.util.Collections; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.boot.Metadata; | ||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
import org.hibernate.testing.transaction.TransactionUtil; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
@TestForIssue(jiraKey = "HHH-14597") | ||
public class ElementCollectionOrphanTest extends BaseCoreFunctionalTestCase { | ||
|
||
@Override | ||
protected String[] getMappings() { | ||
return new String[] { "orphan/elementcollection/student.hbm.xml" }; | ||
} | ||
|
||
@Test | ||
public void setCompositeElementTest() { | ||
TransactionUtil.doInHibernate( | ||
this::sessionFactory, | ||
session -> { | ||
EnrollableClass aClass = new EnrollableClass(); | ||
aClass.setId("123"); | ||
aClass.setName("Math"); | ||
session.save(aClass); | ||
|
||
Student aStudent = new Student(); | ||
aStudent.setId("s1"); | ||
aStudent.setFirstName("John"); | ||
aStudent.setLastName("Smith"); | ||
|
||
EnrolledClassSeat seat = new EnrolledClassSeat(); | ||
seat.setId("seat1"); | ||
seat.setRow(10); | ||
seat.setColumn(5); | ||
|
||
StudentEnrolledClass enrClass = new StudentEnrolledClass(); | ||
enrClass.setEnrolledClass(aClass); | ||
enrClass.setClassStartTime(130); | ||
enrClass.setSeat(seat); | ||
aStudent.setEnrolledClasses(Collections.singleton(enrClass)); | ||
session.save(aStudent); | ||
} | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rnate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrollableClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.hibernate.test.orphan.elementcollection; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "ENROLLABLECLASS") | ||
public class EnrollableClass { | ||
|
||
@Id | ||
@Column(name = "id") | ||
private String id; | ||
|
||
@Column(name = "name") | ||
private String name; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ate-core/src/test/java/org/hibernate/test/orphan/elementcollection/EnrolledClassSeat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hibernate.test.orphan.elementcollection; | ||
|
||
public class EnrolledClassSeat { | ||
private String id; | ||
private int row; | ||
private int column; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public int getRow() { | ||
return row; | ||
} | ||
|
||
public void setRow(int row) { | ||
this.row = row; | ||
} | ||
|
||
public int getColumn() { | ||
return column; | ||
} | ||
|
||
public void setColumn(int column) { | ||
this.column = column; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.hibernate.test.orphan.elementcollection; | ||
|
||
import java.util.Set; | ||
|
||
public class Student { | ||
|
||
private String id; | ||
private String firstName; | ||
private String lastName; | ||
private Set< StudentEnrolledClass > enrolledClasses; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public void setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public void setLastName(String lastName) { | ||
this.lastName = lastName; | ||
} | ||
|
||
public Set<StudentEnrolledClass> getEnrolledClasses() { | ||
return enrolledClasses; | ||
} | ||
|
||
public void setEnrolledClasses(Set<StudentEnrolledClass> enrolledClasses) { | ||
this.enrolledClasses = enrolledClasses; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...-core/src/test/java/org/hibernate/test/orphan/elementcollection/StudentEnrolledClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hibernate.test.orphan.elementcollection; | ||
|
||
public class StudentEnrolledClass { | ||
private EnrollableClass enrolledClass; | ||
private int classStartTime; | ||
private EnrolledClassSeat seat; | ||
|
||
public EnrollableClass getEnrolledClass() { | ||
return enrolledClass; | ||
} | ||
|
||
public void setEnrolledClass(EnrollableClass enrolledClass) { | ||
this.enrolledClass = enrolledClass; | ||
} | ||
|
||
public int getClassStartTime() { | ||
return classStartTime; | ||
} | ||
|
||
public void setClassStartTime(int classStartTime) { | ||
this.classStartTime = classStartTime; | ||
} | ||
|
||
public EnrolledClassSeat getSeat() { | ||
return seat; | ||
} | ||
|
||
public void setSeat(EnrolledClassSeat seat) { | ||
this.seat = seat; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
hibernate-core/src/test/java/org/hibernate/test/orphan/elementcollection/student.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!DOCTYPE hibernate-mapping PUBLIC | ||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" | ||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> | ||
|
||
<hibernate-mapping> | ||
<class name="org.hibernate.test.orphan.elementcollection.Student" table="STUDENT" lazy="true" > | ||
|
||
<id name="id" column="id"> | ||
<generator class="assigned"/> | ||
</id> | ||
|
||
<property name="firstName" column="FIRSTNAME" /> | ||
<property name="lastName" column="LASTNAME" /> | ||
|
||
<set name="enrolledClasses" table="STUDENT_CLASS" lazy="true" fetch="subselect" cascade="all"> | ||
<key column="STUDENT_ID" /> | ||
|
||
<composite-element class="org.hibernate.test.orphan.elementcollection.StudentEnrolledClass" > | ||
<many-to-one class="org.hibernate.test.orphan.elementcollection.EnrollableClass" name="enrolledClass" | ||
column="ENROLLED_CLASS_ID" not-null="true" lazy="false" unique="false" cascade="none" /> | ||
<property name="classStartTime" column="STARTTIME" /> | ||
<many-to-one class="org.hibernate.test.orphan.elementcollection.EnrolledClassSeat" name="seat" | ||
column="ENROLLED_CLASS_SEAT_ID" not-null="true" lazy="false" unique="true" cascade="all-delete-orphan" /> | ||
</composite-element> | ||
</set> | ||
</class> | ||
|
||
<class name="org.hibernate.test.orphan.elementcollection.EnrollableClass" lazy="true" > | ||
|
||
<id name="id" column="id"> | ||
<generator class="assigned"/> | ||
</id> | ||
<property name="name" column="NAME" /> | ||
</class> | ||
|
||
<class name="org.hibernate.test.orphan.elementcollection.EnrolledClassSeat" lazy="true" > | ||
<id name="id" column="id"> | ||
<generator class="assigned"/> | ||
</id> | ||
<property name="row" column="SEAT_ROW" /> | ||
<property name="column" column="SEAT_COLUMN" /> | ||
</class> | ||
</hibernate-mapping> |