1:M relationship in Hibernate and cascading operations

Posted by EugeneP on Stack Overflow See other posts from Stack Overflow or by EugeneP
Published on 2010-04-08T13:09:28Z Indexed on 2010/04/08 13:13 UTC
Read the original article Hit count: 110

Filed under:
|
|

Table SUBCOURSE references COURSE COURSE(id, name) SUBCOURSE(id, course_id, name)

So, 1:M.

Hibernate generates for Course:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "course", cascade = CascadeType.ALL)
    public Set getSubCourses() {
        return this.subCourses;
    }

for Subcourse it generates

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

Now the problem is that cascading does not work as expected. I want to create a collection of SubCourse objects (Set), fill it and then bind it to setSubCourses() of Course object.

Though, having ManyToOne thing in a Subcourses table, I need to manually setCourse() before adding to collection on each object. If I do not do so, an exception is raised.

What can you recommend me?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate