I am trying to clarify the doubt about inverse attribute in Hibernate. I have written a sample program for Student and Course relationship. This is a Many to Many bidirectional relationship between Students and Courses. A student can enroll to many courses and a course can have many students associated with it. So, it is a many-to-many bidirectional relationship.

Given below are the sample java code and hbm xml files. I have the following queries:

I. Consider the following two scenarios:
a. If I mention the inverse=false in Student.hbm.xml file and inverse=true in course.hbm.xml file, then the entries are inserted to Student_Course table.
b. If I mention the inverse=true in Student.hbm.xml file and inverse=false in Course.hbml.xml file then the entries are not inserted to Student_Course table.
Now, I have a question why entries are inserted in case a and not in case b. As per the inverse definition, if we mark inverse as true in one and false in other then the output must be same in both the cases.

II. If I don't add cascade option, then I am getting an exception for persisting an unsaved tranisent. Why it is required to add cascade in relationships?

III. What is the difference between inverse and cascade?

IV. If I don't add inverse attribute in both the XML files, even then the values are inserted in the Student_Course table and there is no difference in the SQL output printed on the console as in the case when inverse is used. So, what is the basic use of inverse attribute?

Thanks Bill for sharing the links. The links have clarified my doubts to some extent.

I have a doubt as below. Please refer the table given below. This table shows the inverse values set as true/false in the Student.hbm.xml for set of courses and in Course.hbm.xml for set of students. And, the last column shows if the data is being inserted in the Student_Course table as Y or N. I am not clear about the last row where inverse=true in Student.hbm.xml and false in Course.hbm.xml and still the data is not inserted in the Student_Course table. As per the inverse definition, the data should be inserted in the table in case one side is true and other is false but why the data is not being inserted in this case. Please suggest.

Student.hbml.xmlCourse.hbm.xmlStudent_Course inverse=true inverse=true N
inverse=false inverse=false Y
inverse=false inverse=true Y
inverse=true inverse=false N

Thanks Bill for sharing the links. The links have clarified my doubts to some extent.

I have a doubt as below. Please refer the table given below. This table shows the inverse values set as true/false in the Student.hbm.xml for set of courses and in Course.hbm.xml for set of students. And, the last column shows if the data is being inserted in the Student_Course table as Y or N. I am not clear about the last row where inverse=true in Student.hbm.xml and false in Course.hbm.xml and still the data is not inserted in the Student_Course table. As per the inverse definition, the data should be inserted in the table in case one side is true and other is false but why the data is not being inserted in this case. Please suggest.

Student.hbml.xml Course.hbm.xml Student_Course
inverse=true inverse=true N
inverse=false inverse=false Y
inverse=false inverse=true Y
inverse=true inverse=false N

subject: Doubt about Inverse Attribute in Hibernate