2010-04-27 55 views
2

之間我有以下表格:冬眠多對多隻保存新的對象和表

學生

Student_Course

現在,當我創建一個學生,我只是想創建一個用戶並向student_course添加一行。 我遇到的問題是,當我在學生中設置一系列課程級聯=「save-update」時,當然也會調用更新。我想知道是否有辦法預防這種情況。

+0

怎麼樣給我們您的映射? – Bozho 2010-04-27 10:44:55

回答

0

如果你真的想這種行爲,你應該分裂您@ManyToMany關係到@一對多,多對一的關係

OBS:不要忘記執行二傳手的

public class Student { 

    private Integer id; 

    public Set<StudentCourse> studentCourseSet = new HashSet<StudentCourse>(); 

    @Id 
    @GeneratedValue 
    public Integer getId() { 
     return this.id; 
    } 

    /** 
     * Because you are using a Set collection 
     * You must provide equals and hashcode implementation in StudentCourse class 
     */ 
    @OneToMany(cascade=CascadeType.ALL) 
    @JoinColumn(name="STUDENT_ID", insertable=false, updatable=false) 
    public Set<StudentCourse> getStudentCourseSet() { 
     return this.studentCourseSet; 
    } 

    /** 
     * add convenience method 
     */ 
    public void addCourse(Course course) { 
     getStudentCourseSet().add(new StudentCourseId(getId(), course.getId())); 
    } 

    /** 
     * Feel free to implement your StudentCourse class outside Student one 
     */ 
    @Entity 
    @Table(name="<TABLE_NAME_GOES_HERE>") 
    public static class StudentCourse { 

     private StudentCourseId studentCourseId; 

     public Student student; 
     public Course course; 

     /** 
      * required no-arg constructor 
      */ 
     public StudentCourse() {} 

     public StudentCourse(StudentCourseId studentCourseId) { 
      this.studentCourseId = studentCourseId; 
     } 

     @EmbeddedId 
     public StudentCourseId getStudentCourseId() { 
      return this.studentCourseId; 
     } 

     @ManyToOne(fetch=FetchType.LAZY) 
     @JoinColumn(name="STUDENT_ID", insertable=false, updatable=false) 
     public Student getStudent() { 
      return this.student; 
     } 

     @ManyToOne(fetch=FetchType.LAZY) 
     @JoinColumn(name="COURSE_ID", insertable=false, updatable=false) 
     public Course getCourse() { 
      return this.course; 
     } 

     @Embeddable 
     public static class StudentCourseId implements Serializable { 

      private Integer studentId; 
      private Integer courseId; 

      /** 
       * required no-arg constructor 
       */ 
      public StudentCourseId() {} 

      public StudentCourseId(Integer studentId, Integer courseId) { 
       this.studentId = studentId; 
       this.courseId = courseId; 
      } 

      @Column(name="STUDENT_ID", nullable=false) 
      public Integer getStudentId() { 
       return this.studentId; 
      } 

      @Column(name="COURSE_ID", nullable=false) 
      public Integer getCourseId() { 
       return this.courseId; 
      } 

      // required equals and hashcode 
      public boolean equals(Object o) { 
       if(o == null) 
        return false; 

       if(!(o instanfeof StudentCourseId)) 
        return false; 

       StudentCourseId other = (StudentCourseId) o; 
       if(!(getStudentId().equals(o.getStudentId()))) 
        return false; 

       if(!(getCourseId().equals(o.getCourseId()))) 
        return false; 

       return false; 
      } 

      public int hashcode() { 
       // hashcode impl goes here 
      } 

     } 

    } 

}