2009-07-20 91 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

相關問題