由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问一个Many to Many的设计问题
相关主题
ask a spring framework question问个JPA的问题
Questions on EJB3 Persistence/HibernateJava EE 习题 1
有多少人对annotation这个东西不满,请举手!!JPA Criteria API select question
谁来给简单的讲讲JAASannotation question
j2ee without EJBspring Annotation based configuration
hibernate高手求助现在有些迷惑了
其实有没有而已解释一下java ee是什么Java Play Framework 和 Spring 比较一下? (转载)
唉进了一家烂公司J2EE入门
相关话题的讨论汇总
话题: many话题: student话题: course话题: scores话题: courses
进入Java版参与讨论
1 (共1页)
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层去管理好了。
1 (共1页)
进入Java版参与讨论
相关主题
J2EE入门j2ee without EJB
when will ejb3 reach final release?hibernate高手求助
Any good book recommendation for EJB?其实有没有而已解释一下java ee是什么
j2ee现在流行什么?唉进了一家烂公司
ask a spring framework question问个JPA的问题
Questions on EJB3 Persistence/HibernateJava EE 习题 1
有多少人对annotation这个东西不满,请举手!!JPA Criteria API select question
谁来给简单的讲讲JAASannotation question
相关话题的讨论汇总
话题: many话题: student话题: course话题: scores话题: courses