简体   繁体   English

休眠多对多关系意外更新

[英]hibernate many-to-many relationship updating unexpectingly

I have two classes: 我有两节课:

class TrainingCourse {
    Integer id;
    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name = "TrainingCourseClass", joinColumns = { @JoinColumn(name = "CourseID") }, inverseJoinColumns = { @JoinColumn(name = "ClassID") })
    private Set<TrainingClass> trainingClasses; 
}

class TrainingClass {
    Integer id;
}

In the database they are mapped using a join table. 在数据库中,它们使用联接表进行映射。 So this is a unidirectional relationship. 因此,这是单向关系。

From the UI, when a TrainingCourse is created, a list of previously created TrainingClasses are selected from the UI. 从UI中,当创建TrainingCourse时,将从UI中选择以前创建的TrainingClasses的列表。

Now if I create the TrainingCourse, then it automatically updates the associated TrainingClasses also. 现在,如果我创建了TrainingCourse,那么它也会自动更新关联的TrainingClasses。 But trainingClass is independent of TrainingCourse and can exist independently. 但是trainingClass独立于TrainingCourse,可以独立存在。 So TrainingClasses are created and updated separately from the TrainingCourse. 因此,TrainingClasses是与TrainingCourse分开创建和更新的。 So saving the TrainingCourse should save data in the TrainingCourse table and it will also save the association in the join Table TrainingCourseClass. 因此,保存TrainingCourse应该将数据保存在TrainingCourse表中,并将关联也保存在联接表TrainingCourseClass中。 Nothing should happen in the table TrainingClass. 表TrainingClass中什么也不会发生。

However if I add these to the columns: 但是,如果我将这些添加到列中:

nullable=false, updatable=false and CascadeType.REMOVE


@OneToMany(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
    @JoinTable(name = "TrainingCourseClass", joinColumns = { @JoinColumn(name = "CourseID", nullable=false, updatable=false) }, inverseJoinColumns = { @JoinColumn(name = "ClassID", nullable=false, updatable=false) })
    private Set<TrainingClass> trainingClasses; 

Then the problem is fixed ie creating trainingCourse doesn't update the trainingClass table. 然后问题解决了,即创建trainingCourse不会更新trainingClass表。 Now I am not 100% sure whether it is the right solution or how it is working to solve the problem. 现在,我不确定100%是正确的解决方案还是解决问题的方法。 There is also another thing called MappedBy. 还有另一件事叫做MappedBy。 I am not sure whether this is relevant here. 我不确定这是否与此有关。

I just used it as a guess and it is working. 我只是将其用作猜测,并且它正在工作。 Moreover, this seems to be really a many-to-many relationship ie The same class can belong to many courses and one course can include many classes. 而且,这似乎确实是多对多的关系,即同一课程可以属于许多课程,而一个课程可以包含许多课程。 But one-to-many relationship is also working. 但是,一对多关系也在起作用。 This is not very convincing. 这不是很令人信服。 The trainingclass is really unaware of what training courses include it. 培训班实际上并不知道其中包括哪些培训课程。 It looks like the difference between one-to-many and many-to-many is like whether or not to have bidirectional pointers to each other. 似乎一对多和多对多之间的区别就像是否具有双向指针。

Hence please suggest whether the above approach is correct to prevent updating the trainingclass while creating the trainingcourse. 因此,请提出上述方法是否正确,以防止在创建培训课程时更新培训课程。

Thanks 谢谢

Your first mapping uses cascade = ALL . 您的第一个映射使用cascade = ALL That means that every operation you make on a TrainingCourse (persist, merge, remove, etc.) will also be applied on the associated TrainingClass. 这意味着您对TrainingCourse进行的每个操作(持续,合并,删除等)也将应用于关联的TrainingClass。 That's precisely what you don't want, if I understand correctly. 如果我理解正确,那正是你所不想要的。 So just don't set any cascade to this association. 因此,请勿对此关联设置任何级联。

Regarding OneToMany vs. ManyToMany: if what you really want is a OneToMany (ie a TraningClass should not be associated with more than one TrainingCourse), then you should have a unique contraint on the TrainingCourseClass.ClassID column. 关于OneToMany与ManyToMany:如果您真正想要的是OneToMany(即,一个TraningClass不应与一个以上TrainingCourse相关联),那么您在TrainingCourseClass.ClassID列上应该有一个唯一的约束。 That's what guarantees that the association is a OneToMany and not a ManyToMany. 这就是保证该关联是OneToMany而不是ManyToMany的原因。

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

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