简体   繁体   English

一对多休眠保存级联问题

[英]OneToMany Hibernate Save Cascade Problems

I was working with Hibernate and OneToMany Relationship, i came across a weird behavior today , although i did it purposely but i was not expecting this behavior 我与Hibernate和OneToMany Relationship合作时,今天遇到了奇怪的行为, 尽管我是故意这样做的,但我并不期望这种行为

College.java College.java

@Entity
@Table(name="COLLEGE")
public class College implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="COLLEGE_XID")
    private Long collegeId;

    @Column(name="COLLEGE_NAME")
    private String collegeName;

    @OneToMany(mappedBy="college",cascade = CascadeType.ALL) 
    private List<Student> studentList = new ArrayList<Student>();
    //... with get and setters
}

Student.java 学生.java

@Entity
@Table(name = "STUDENT")
public class Student implements Serializable    {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="STUDENT_XID")
    private Long studentId;

    @Column(name="STUDENT_NAME")
    private String studentName;

    @ManyToOne
    @JoinColumn(name="COLLEGE_XID",nullable=false)
    private College college;
    //... with get and setters
}

Application.java 应用程序

    College college1 = new BdCollege();
    college1.setCollegeName("C_1");

    College college2 = new BdCollege();
    college2.setCollegeName("C_2");

    Student student1 = new BdStudent();
    student1.setStudentName("S_1_C_2");

    CollegeDAO collegeDAO = new  CollegeDAO();

    // setting student 1 Parent to College 1        
    student1.setCollege(college1);   // <---- Here the Issue  ... student 1 as college 1 child

    // BUT assigning Student 1 as Child of Collge 2 in List 
    // i did it purposely to test the behaviour
    college2.getStudentList().add(student1);  // <---- Here the Issue ... but assigning it in the list of College 2 ... ws expecting Exception 

     // Saved College 1 & 2
     collegeDAO.save(college1);
     collegeDAO.save(college2);

      // Saved Successfully BUT

RESULT COLLEGE Table 结果学院表

 COLLEGE_XID     COLLEGE_NAME    
 --------------  --------------- 
 1               C_1             
 2               C_2 

STUDENT Table 学生表

 STUDENT_XID     STUDENT_NAME     COLLEGE_XID    
 --------------  ---------------  -------------- 
 1               S_1_C_2          1   

I was expecting some exception as assignment from child and parents mis-matched in college - student example and another thing that stucked me were the query generated by hibernate 我期待一些例外,因为孩子和父母在大学里的作业不匹配-学生的例子和让我感到困扰的另一件事是休眠模式生成的查询

Hibernate: insert into COLLEGE (COLLEGE_NAME) values (?)
Hibernate: insert into COLLEGE (COLLEGE_NAME) values (?)
Hibernate: insert into STUDENT (COLLEGE_XID, STUDENT_NAME) values (?, ?)

ie it save College 1 first, Then College 2 Second and Saving Student as Child of College 2 but in the result it appears as child of College 1 也就是说,它首先保存College 1,然后保存College 2,然后将学生保存为College 2的孩子,但结果显示为College 1的孩子

Its because you added mappedBy attribute and made student as owner of association. 这是因为您添加了mappingBy属性并使学生成为协会的所有者。 Since Student entity is the owner, it will maintain the relationship and hibernate will ignore changes on College side. 由于学生实体是所有者,因此它将维持关系,并且休眠状态将忽略学院方面的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM