A**o 发帖数: 1550 | 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 ( | m****r 发帖数: 6639 | 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.
| A**o 发帖数: 1550 | 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.
| r*******g 发帖数: 8 | 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... | F****n 发帖数: 3271 | 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.
| m******t 发帖数: 2416 | 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")
| t*******e 发帖数: 684 | 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")
| A**o 发帖数: 1550 | 8 谢谢大家,我基本上使用Foxman的办法,三个Objects,两个一对多的关系,剩下的东
西还是让business层去管理好了。 |
|