2013-12-20 89 views
0

我有以下的MySQL表。這裏的想法是,賽道和過程是主要關鍵。和在一個軌道中的過程可以取決於在「相同的軌道」另一個過程因此我已經創建了一個複合FK上(track_iddepends_on_course_id休眠複合自引用外鍵映射

CREATE TABLE IF NOT EXISTS `testdb_1`.`track_course` (
`track_id` INT UNSIGNED NOT NULL , 
`course_id` INT UNSIGNED NOT NULL , 
`depends_on_course_id` INT UNSIGNED NULL , 
PRIMARY KEY (`track_id`, `course_id`) , 
INDEX `tc_tbl_dependent_crs_fk_idx` (`track_id` ASC, `depends_on_course_id` ASC) , 
CONSTRAINT `tc_tbl_dependent_crs_fk` 
FOREIGN KEY (`track_id` , `depends_on_course_id`) 
REFERENCES `testdb_1`.`track_course` (`track_id` , `course_id`)) 

和下面類是由休眠工具自動生成

@Entity 
@Table(name = "track_course", catalog = "testdb_1") 
public class TrackCourse implements java.io.Serializable { 

    private TrackCourseId id; 
    private TrackCourse trackCourse; 
    private Set<TrackCourse> trackCourses = new HashSet<TrackCourse>(0); 

    public TrackCourse() { 
    } 

    public TrackCourse(TrackCourseId id, TrackCourse trackCourse) { 
     this.id = id; 
     this.trackCourse = trackCourse; 
    } 

    public TrackCourse(TrackCourseId id, TrackCourse trackCourse, 
      Set<TrackCourse> trackCourses) { 
     this.id = id; 
     this.trackCourse = trackCourse; 
     this.trackCourses = trackCourses; 
    } 

    @EmbeddedId 
    @AttributeOverrides({ 
      @AttributeOverride(name = "trackId", column = @Column(name = "track_id", nullable = false)), 
      @AttributeOverride(name = "courseId", column = @Column(name = "course_id", nullable = false)) }) 
    public TrackCourseId getId() { 
     return this.id; 
    } 

    public void setId(TrackCourseId id) { 
     this.id = id; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumns({ 
      @JoinColumn(name = "track_id", referencedColumnName = "track_id", insertable = false, updatable = false), 
      @JoinColumn(name = "depends_on_course_id", referencedColumnName = "course_id", insertable = false, updatable = false) }) 
    public TrackCourse getTrackCourse() { 
     return this.trackCourse; 
    } 

    public void setTrackCourse(TrackCourse trackCourse) { 
     this.trackCourse = trackCourse; 
    } 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "trackCourse", cascade= CascadeType.ALL) 
    public Set<TrackCourse> getTrackCourses() { 
     return this.trackCourses; 
    } 

    public void setTrackCourses(Set<TrackCourse> trackCourses) { 
     this.trackCourses = trackCourses; 
    } 

} 

我想按如下方式寫入此表。

TrackCourse tc1 = new TrackCourse(new TrackCourseId(11, 1), null); 
TrackCourse tc2 = new TrackCourse(new TrackCourseId(11, 2), tc1); 
TrackCourse tc4 = new TrackCourse(new TrackCourseId(11, 4), tc1); 

Set<TrackCourse> trackCourses = new HashSet<TrackCourse>(); 
trackCourses.add(tc2); 
trackCourses.add(tc4); 
tc1.setTrackCourses(trackCourses); 
session.save(tc1); // I expect this statement to create three rows with following values 

track_id course_id depends_on_course_id 
11,   1,   null 
11   2   1 
11   4   1 

但我得到了「depends_on_course_id列」的所有空,我想有一些錯誤的方法getTrackCourse的映射,但我無法弄清楚什麼? 任何幫助,非常感謝。

回答

0

似乎getTrackCourse()有空的cascadeType,所以默認情況下沒有操作級聯。

Btw。我會建議使用生成的密鑰而不是自然密鑰,這會使事情更加複雜。

+0

那我以前不幫助,但如果我明確地添加一列映射到我的課,然後它工作(但它也查詢更加複雜)@Column(NAME =「depends_on_course_id」) \t公共整數getDependency(){ \t \t if(this.trackCourse!= null) \t \t \t return this.trackCourse.getId()。getCourseId(); \t \t else \t \t \t return null; \t} – Rahul