开发者

hibernate many to many only save the new object and between table

开发者 https://www.devze.com 2022-12-28 02:17 出处:网络
I have the following tables: Student Student_Course 开发者_如何转开发Course Now when I create a student I only want to create a user and add a row to student_course.

I have the following tables:

Student

Student_Course

开发者_如何转开发

Course

Now when I create a student I only want to create a user and add a row to student_course. The "problem" I have is that when I set the set of courses in Student to cascade="save-update" that an update is also invoked on course. I was wondering if there was a way to prevent this.


If you really want this kind of behavior, you should split your @ManyToMany relationship into @OneToMany-ManyToOne relationship

obs: do not forget implement setter's

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
            }

        }

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号