2009-07-20 75 views
13

我有这两个类(表)可以添加额外的字段到@ManyToMany Hibernate额外的表吗?

@Entity 
@Table(name = "course") 
public class Course { 

    @Id 
    @Column(name = "courseid") 
    private String courseId; 
    @Column(name = "coursename") 
    private String courseName; 
    @Column(name = "vahed") 
    private int vahed; 
    @Column(name = "coursedep") 
    private int dep; 
    @ManyToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "student_id")) 
    private Set<Student> student = new HashSet<Student>(); 
//Some setter and getter 

这一个:

@Entity 
    @Table(name = "student") 
    public class Student { 

     @Id 
     @Column(name="studid") 
     private String stId; 
     @Column(nullable = false, name="studname") 
     private String studName; 
     @Column(name="stmajor") 
     private String stMajor; 
     @Column(name="stlevel", length=3) 
     private String stLevel; 
     @Column(name="stdep") 
     private int stdep; 

     @ManyToMany(fetch = FetchType.LAZY) 
     @JoinTable(name = "student_course" 
       ,joinColumns = @JoinColumn(name = "student_id") 
       ,inverseJoinColumns = @JoinColumn(name = "course_id") 
     ) 
     private Set<Course> course = new HashSet<Course>(); 
//Some setter and getter 

这段代码运行额外的表在数据库(student_course)创建后,现在我想知道我怎么能增加额外的领域在这个表像(等级,日期和...(我的意思是student_course表)) 我看到一些解决方案,但我不喜欢他们,我也有一些问题,他们说:

First Sample

+0

您可以使用\ @OrderColumn添加一个额外的列,这是一个int列,用于存储many2many关系的排序顺序。我希望他们会添加一个\ @TempolalColumns来添加fromDate和toDate字段 – 2013-08-09 06:43:48

回答

16

如果您在链接表(STUDENT_COURSE)上添加了额外的字段,您必须根据skaffman的回答或其他方式选择一种方法,如下所示。

有一个方法,把链接表(STUDENT_COURSE)的行为就像根据@Embeddable:

@Embeddable 
public class JoinedStudentCourse { 

    // Lets suppose you have added this field 
    @Column(updatable=false) 
    private Date joinedDate; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="STUDENT_ID", insertable=false, updatable=false) 
    private Student student; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="COURSE_ID", insertable=false, updatable=false) 
    private Course course; 

    // getter's and setter's 

    public boolean equals(Object instance) { 
     if(instance == null) 
      return false; 

     if(!(instance instanceof JoinedStudentCourse)) 
      return false; 

     JoinedStudentCourse other = (JoinedStudentCourse) instance; 
     if(!(student.getId().equals(other.getStudent().getId())) 
      return false; 

     if(!(course.getId().equals(other.getCourse().getId())) 
      return false; 

     // ATT: use immutable fields like joinedDate in equals() implementation 
     if(!(joinedDate.equals(other.getJoinedDate())) 
      return false; 

     return true; 
    } 

    public int hashcode() { 
     // hashcode implementation 
    } 

} 

所以你必须在这两个学生和课程类

public class Student { 

    @CollectionOfElements 
    @JoinTable(
     [email protected](name="STUDENT_COURSE"), 
     [email protected](name="STUDENT_ID") 
    ) 
    private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>(); 

} 

public class Course { 

    @CollectionOfElements 
    @JoinTable(
     [email protected](name="STUDENT_COURSE"), 
     [email protected](name="COURSE_ID") 
    ) 
    private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>(); 

} 

记住:@可嵌入类的生命周期必须与拥有的实体类(学生和课程)绑定,因此请妥善保管。

意见:由于@ManyToMany映射 - 级联操作中的某些限制,Hibernate团队支持这两种方法(@OneToMany(skaffman's answer)或@CollectionsOfElements)。

问候,

7

student_course表格纯粹是为了记录两个实体之间的关联。它由Hibernate管理,并且不能包含其他数据。

您想要记录的数据类型需要建模为另一个实体。也许你可以在Course和StudentResult(包含成绩等)之间建立一对多的关联,然后在StdentResult和Student之间建立多对一的关联。

+0

Tanx男人我认为它 – Am1rr3zA 2009-07-20 13:19:31

3

放弃多对多,创建一个名为StudentCourseRelationship的类,并在StudentCourseRelationship上设置一个给学生和课程的manys。

你可以把各种各样的东西就可以了,像DateEnrolled,DateKickedOut等等,等等

IMO的许多一对多的映射是有点一个反面的。

+0

Tanx男人我认为它 – Am1rr3zA 2009-07-20 13:18:27

+1

DateGotToGripsWithAssociations怎么样? :) – skaffman 2009-07-20 13:34:20