Redian新闻
>
问一个Many to Many的设计问题
avatar
问一个Many to Many的设计问题# Java - 爪哇娇娃
A*o
1
一个简单的例子:
student - course - scores.
student has courses;
course has students;
and each student has a score on each course.
so, students and course are many to many.
now that scores make the many to many relationship into another entity.
and the question is, in hibernate or ejb3 world,
how do i map them? so that i can find which courses a student in
and which students in courses, and what scores are like.
should i have students with scores;
and course with scores;
and also students with courses (
avatar
m*r
2
hybernate我不太了解. 但是这个db table来说, 应该有students, courses, 然后有
一个students_courses, student id 和course id是foreign key, 然后有一个score
column.

【在 A**o 的大作中提到】
: 一个简单的例子:
: student - course - scores.
: student has courses;
: course has students;
: and each student has a score on each course.
: so, students and course are many to many.
: now that scores make the many to many relationship into another entity.
: and the question is, in hibernate or ejb3 world,
: how do i map them? so that i can find which courses a student in
: and which students in courses, and what scores are like.

avatar
A*o
3
thanks. I have no problem in coming up with the db schema.
however, i'm a little trouble in object modeling.
and since i'm stuck in ejb for now. i'm not sure
if there is an elegant way for dealing these relations.
in jdbc days, i'm just assigning each functions with my own sql query.
but i'm still a newbie in OR mapping.

【在 m****r 的大作中提到】
: hybernate我不太了解. 但是这个db table来说, 应该有students, courses, 然后有
: 一个students_courses, student id 和course id是foreign key, 然后有一个score
: column.

avatar
r*g
4
I would approach it with students-courses with many-to-many, and then have
scores as a direct 1-1 mapping contained within the student, since logically
it belongs to student more with each student's course has one score...
avatar
F*n
5
You can map both Student and Course to Score as Many to one.
@Entity
@IdClass(ScoreId.class)
public class Score {
@Id
private int studentId;
@Id
private int courseId;
@ManyToOne
@JoinColumn(name="studentId")
private Student student;
@ManyToOne
@JoinColumn(name="courseId")
private Course course
}
@Entity
public class Student {
...
@OneToMany (mappedBy="student")
private Collection scores;
}
@Entity
public class Course {
...
@OneToMany(

【在 A**o 的大作中提到】
: thanks. I have no problem in coming up with the db schema.
: however, i'm a little trouble in object modeling.
: and since i'm stuck in ejb for now. i'm not sure
: if there is an elegant way for dealing these relations.
: in jdbc days, i'm just assigning each functions with my own sql query.
: but i'm still a newbie in OR mapping.

avatar
m*t
6
+1, except for the obnoxious annotations though. ;-)
I would also name the join class something more generic than Score.
For instance, Attendence or Enrollment, in case more attributes than
the score are discovered later.

【在 F****n 的大作中提到】
: You can map both Student and Course to Score as Many to one.
: @Entity
: @IdClass(ScoreId.class)
: public class Score {
: @Id
: private int studentId;
: @Id
: private int courseId;
: @ManyToOne
: @JoinColumn(name="studentId")

avatar
t*e
7
Great response. OP needs an explicit link table.

【在 F****n 的大作中提到】
: You can map both Student and Course to Score as Many to one.
: @Entity
: @IdClass(ScoreId.class)
: public class Score {
: @Id
: private int studentId;
: @Id
: private int courseId;
: @ManyToOne
: @JoinColumn(name="studentId")

avatar
A*o
8
谢谢大家,我基本上使用Foxman的办法,三个Objects,两个一对多的关系,剩下的东
西还是让business层去管理好了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。