4

Can someone explain the use of inverse in the xml mapping file, I am reading the tutorial but failing to understand its use in the mapping file??

Thanks

Noor
  • 19,638
  • 38
  • 136
  • 254

4 Answers4

8

Inverse just decides which entity in a relationship is responsible for updating the database for reflecting the association.

Assume a one to many bidirectional association. There are two classes in the code A and B, A contains a set of B, B maintains a reference to A. At the database level, there is only one foreign key to be updated, the table for B contains a column to primary key of A.

In this case, assume we put the inverse = true on the set side. This implies that just adding an entity to the set will not fire the foreign key update. Because the respnsibility to update the foreign key rests with B. So, adding a B object to the set that A maintains is not enough to update the foreign key column. objectA.addToSetOfB(objectB) will not affect the foreign key.

Only when B is given a reference to A, will the foreign key in the table for B be updated. So, objectB.setA(objectA) will surely update the foreign key and actually setup the relationship.

I think the same concept will carry to the many to many relationships as well.

Abhijeet Kashnia
  • 12,290
  • 8
  • 38
  • 50
3

If a collection is marked as "inverse", then Hibernate will not execute any SQL to maintain the collection in the database.

For example, one-to-many collections are often (in my experience, practically always) marked as inverse: the "many" entities (members of the collection) have a column with the parent's ID (mapped as a many-to-one property), and simply creating one of those entities means that it will be implicitly included in the collection, so no need to explicitly update them.

If using a many-to-many collection (which of course usually occur in pairs), one of the collections needs to be marked as "inverse", otherwise Hibernate will try to create the join table entries representing the collection twice.

araqnid
  • 127,052
  • 24
  • 157
  • 134
1

In many-to-many relationships, you follow the direction of your join.

Let's have an example :

  • Student(IdStudent, StudentName)

  • Course(IdCourse, CourseName)

A student can follow one or many courses, so we'll have a joint table between Student and Course, named StudentCourse(IdStudent,IdCourse).

The inverse attribute needs to be positionned on the Course half, because it will indicate to Hibernate (which doesn't know much about the StudentCourse table) to correctly build its queries.

If you put the inverse attribute to true on the Student half, Hibernate will think that the Joint Table is CourseStudent(IdCourse,IdStudent) !!

It's the same behaviour for the one-to-many relationsships.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
0

inverse tells hibernate have to manage foreign keys so that you do not get problem with reference entities. if you define a relation bidirectional you can navigate on both sides of the object. inverse flag enables you to set the relation correctly(to avoid constraint violations). hibernate do not know how to crate update inser statements if you do not define inverse flag. Making one side of a collection tells hibernate that it is mirror of the other side. that is always required for hibernate to transform java into sql code.

The rules are straightforward:

Rules for bidirectional relations:

  • All bi-directional associations need one side as inverse.
  • one-to-many => association it has to be the many-side
  • many-to-many => association you can select either side.
Community
  • 1
  • 1
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30