Hibernate many for many only save new object and between tables

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 line to student_course. The "problem" I have is that when I set a set of courses in Student for cascade = "save-update", that update is also called on the course. I was wondering if there is a way to prevent this.

+2


a source to share


1 answer


If you really want this behavior , you should split the @ManyToMany relationship into @ OneToMany-ManyToOne relationships

obs: don't forget to install the setter



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


a source







All Articles