File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Doubt about Inverse Attribute in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Doubt about Inverse Attribute in Hibernate" Watch "Doubt about Inverse Attribute in Hibernate" New topic
Author

Doubt about Inverse Attribute in Hibernate

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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?

Sample Code




Student.hbm.xml file:



Course.hbml.xml
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

See if these threads help

http://www.coderanch.com/t/595728/ORM/databases/Newbie-Hibernate-OneToMany-owning-side#2716915
http://www.coderanch.com/t/593523/ORM/databases/JPA-persist-relationship-owning-side


[How To Ask Questions][Read before you PM me]
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Here you have a cheat sheet with JPA mappings:
http://leakfromjavaheap.blogspot.com/2013/01/basic-jpa-mappings.html


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140



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
 
jQuery in Action, 2nd edition
 
subject: Doubt about Inverse Attribute in Hibernate