File tree Expand file tree Collapse file tree 4 files changed +33
-4
lines changed
hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone Expand file tree Collapse file tree 4 files changed +33
-4
lines changed Original file line number Diff line number Diff line change 55package org .hibernate .orm .test .annotations .idmanytoone ;
66
77import java .io .Serializable ;
8+ import java .util .HashSet ;
89import java .util .Set ;
10+
11+ import jakarta .persistence .CascadeType ;
912import jakarta .persistence .Entity ;
1013import jakarta .persistence .GeneratedValue ;
1114import jakarta .persistence .Id ;
@@ -25,8 +28,8 @@ public class Course implements Serializable {
2528
2629 private String name ;
2730
28- @ OneToMany (mappedBy = "course" )
29- private Set <CourseStudent > students ;
31+ @ OneToMany (mappedBy = "course" , cascade = CascadeType . ALL )
32+ private Set <CourseStudent > students = new HashSet <>() ;
3033
3134 public Course () {
3235 }
Original file line number Diff line number Diff line change 1212import jakarta .persistence .JoinColumn ;
1313import jakarta .persistence .ManyToOne ;
1414import jakarta .persistence .Table ;
15+ import org .hibernate .annotations .processing .Exclude ;
1516
1617/**
1718 * @author Alex Kalashnikov
1819 */
1920@ Entity
2021@ Table (name = "idmanytoone_course_student" )
22+ @ Exclude // Avoid generating an IdClass through the annotation processor. See https://hibernate.atlassian.net/browse/HHH-18829
2123public class CourseStudent implements Serializable {
2224
2325 @ Id
Original file line number Diff line number Diff line change 1414import org .hibernate .boot .model .naming .ImplicitNamingStrategyJpaCompliantImpl ;
1515import org .hibernate .cfg .Configuration ;
1616
17+ import org .hibernate .testing .orm .junit .Jira ;
1718import org .hibernate .testing .orm .junit .JiraKey ;
1819import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
1920import org .junit .Test ;
@@ -78,6 +79,26 @@ public void testCriteriaRestrictionOnIdManyToOne() {
7879 } );
7980 }
8081
82+ @ Test
83+ @ Jira ("https://hibernate.atlassian.net/browse/HHH-11026" )
84+ public void testMerge () {
85+ inTransaction ( s -> {
86+ Student student = new Student ();
87+ student .setName ( "s1" );
88+ Course course = new Course ();
89+ course .setName ( "c1" );
90+ s .persist ( student );
91+ s .persist ( course );
92+
93+ CourseStudent courseStudent = new CourseStudent ();
94+ courseStudent .setStudent ( student );
95+ courseStudent .setCourse ( course );
96+ student .getCourses ().add ( courseStudent );
97+ course .getStudents ().add ( courseStudent );
98+ s .merge ( student );
99+ } );
100+ }
101+
81102 @ Override
82103 protected Class [] getAnnotatedClasses () {
83104 return new Class [] {
Original file line number Diff line number Diff line change 55package org .hibernate .orm .test .annotations .idmanytoone ;
66
77import java .io .Serializable ;
8+ import java .util .HashSet ;
89import java .util .Set ;
10+
11+ import jakarta .persistence .CascadeType ;
912import jakarta .persistence .Entity ;
1013import jakarta .persistence .GeneratedValue ;
1114import jakarta .persistence .Id ;
@@ -25,8 +28,8 @@ public class Student implements Serializable {
2528
2629 private String name ;
2730
28- @ OneToMany (mappedBy = "student" )
29- private Set <CourseStudent > courses ;
31+ @ OneToMany (mappedBy = "student" , cascade = CascadeType . ALL )
32+ private Set <CourseStudent > courses = new HashSet <>() ;
3033
3134 public Student () {
3235 }
You can’t perform that action at this time.
0 commit comments