Hibernate双向多对多实现
我是休眠新手,我想在休眠中实现类似的东西。为了能够做到这一点,我在设置 xml 映射文件时遇到了问题。如果有人可以帮助我,那就太好了,因为这只是我想做的概念证明,我有很多复杂的事情要做。谢谢,
public class Course implements java.io.Serializable
{
private long courseId;
private String courseName;
private Set <Student> Stu = new HashSet <Student>();
}
public class Student implements java.io.Serializable
{
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>();
}
但是在数据库中,我想创建 3 个表 Student,Course 和 StudentCourse
Student----->StudentCourse<------Course
StudentId StudentId CourseId
CourseId
我想要的是,当我这样做时,
Course C1=(Course)session.get(Course.class,CourseId)
我会得到指定的课程,并且通过这样做,
Set <Student> StudentsEnrolled=C1.getStu();
我会让所有学生都注册该
课程 当我这样做时,
Student S1=(Student)session.get(Student.class,StudentId)
我会得到指定的学生,并且通过这样做,
Set <Course> CoursesEnrolled=S1.getCourses();
我会得到指定学生已修读的所有课程
I am new to hibernate and I want to implement something like this in hibernate. To be able to do this, I am getting problem in setting the xml mapping file. If someone can help me, it would be very nice as this is just of proof of concept I am trying to do, I have much complicated things to do.Thanks
public class Course implements java.io.Serializable
{
private long courseId;
private String courseName;
private Set <Student> Stu = new HashSet <Student>();
}
public class Student implements java.io.Serializable
{
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>();
}
But in the database, I want 3 table to be created
Student,Course and StudentCourse
Student----->StudentCourse<------Course
StudentId StudentId CourseId
CourseId
What I want is that when I do
Course C1=(Course)session.get(Course.class,CourseId)
I get the specified course and by doing
Set <Student> StudentsEnrolled=C1.getStu();
I get all students enrolled in that course
Similary
When I do
Student S1=(Student)session.get(Student.class,StudentId)
I get the specified student and by doing
Set <Course> CoursesEnrolled=S1.getCourses();
I get all courses the specified student has taken
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个
好吧,如果你像这样镜像另一个类中的集合,这不起作用吗?
Take a look at this
Well if you mirror the set in the other class like this doesn't it work?
您正在寻找的内容与此处所述的示例非常相似:
http://technicalmumbojumbo.wordpress.com/ 2007/09/25/investigating-hibernate-associations-many-to-many/
它从单向关系开始,最后定义双向关系的映射。
希望这有帮助,
奥克塔夫
=======================================
不要忘记您必须决定哪个实体是此关系的所有者:学生或课程。
一旦您决定了这一点,“拥有”实体的 Hibernate 映射需要将“inverse”标志设置为 true。
如果您在文章末尾检查 Phone 实体的 Hibernate 映射,这正是上面链接中描述的方式。
What you are looking for is very similar with the example stated here:
http://technicalmumbojumbo.wordpress.com/2007/09/25/investigating-hibernate-associations-many-to-many/
It starts with a unidirectional relationship, and at the end it defines the mappings for the bidirectional one.
Hope this helps,
Octav
=====================================
Don't forget you have to decide which entity is the owner of this relationship: Student or Course.
Once you've decided that, the Hibernate mapping of the "owned" entity needs to have the "inverse" flag set to true.
That's exactly how it's described in the link above, if you check the Hibernate mapping for the Phone entity, at the end of the article.