许多到许多休眠反面忽略 [英] Many to Many hibernate inverse side ignored

查看:87
本文介绍了许多到许多休眠反面忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在阅读hibernate文档。

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html


使用@ManyToMany
注释逻辑地定义多对多关联。您还必须使用@JoinTable注释来描述关联表和
连接条件。如果关联是
双向,则一方必须是所有者,一方必须是
的逆向结束(即,在更新关联表中的关系
值时它将被忽略):

我了解除上次以外的所有内容


(例如,更新关联表中的关系值时它将被忽略)。 这是什么意思?例子?

解决方案

假设您有以下实体:

<$ p $ @Entity
public class Student {
@ManyToMany
private Set< Course>培训班;
...
}

@Entity
public class Course {
@ManyToMany(mappedBy =courses)
private Set<学生>学生们;
...
}

所有者一方是学生(因为它没有没有 mappedBy 属性)。反面是Course((因为它具有 mappedBy 属性)。



如果执行以下操作:

 课程course = session.get(Course.class,3L); 
Student student = session.get(Student。 class,4L);
student.getCourses()。add(course);

Hibernate将为联系表中的学生4和课程3添加条目,因为您已更新联系的所有者端( student.courses )。



如果您执行以下操作:

 课程course = session.get(Course.class,3L ); 
Student student = session.get(Student.class,4L);
course.getStudents().add(student);

什么都不会发生,因为你更新了关联的反面( course.students ),但忽略更新所有者方.Hibernate只考虑所有者方。


Hi am reading the hibernate documentation.

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html

A many-to-many association is defined logically using the @ManyToMany annotation. You also have to describe the association table and the join conditions using the @JoinTable annotation. If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be ignored when updating the relationship values in the association table):

I understand everything but the last

(ie. it will be ignored when updating the relationship values in the association table).

What does this mean? Example?

解决方案

Suppose you have the following entities:

@Entity
public class Student {
    @ManyToMany
    private Set<Course> courses;
    ...
}

@Entity
public class Course {
    @ManyToMany(mappedBy = "courses")
    private Set<Student> students;
    ...
}

The owner side is Student (because it doesn't have the mappedBy attribute). The inverse side is Course ((because it has the mappedBy attribute).

If you do the following:

Course course = session.get(Course.class, 3L);
Student student = session.get(Student.class, 4L);
student.getCourses().add(course);

Hibernate will add an entry for student 4 and course 3 in the join table because you updated the owner side of the association (student.courses).

Whereas if you do the following:

Course course = session.get(Course.class, 3L);
Student student = session.get(Student.class, 4L);
course.getStudents().add(student);

nothing will happen, because uou updated the inverse side of the association (course.students), but neglected to updated the owner side. Hibernate only considers the owner side.

这篇关于许多到许多休眠反面忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆