What does inverse option does in Hibernate?

The inverse keyword is always declare in one-to-many and many-to-many relationship (many-to-one doesn’t has inverse keyword), it means which side is responsible to take care of the relationship.

In Hibernate, only the “owner of relationship” should maintain the relationship, and the inverse keyword is created to defines which side is the owner to maintain the relationship. However the inverse keyword itself is not verbose enough, I would suggest change the keyword to “owner of relationship“.

In short, inverse=”true” means this is the relationship owner, and inverse=”false” (default) means it’s not.

Let us try to understand this with an example:

File : Employee.hbm.xml

<hibernate-mapping>
<class name="com.tuturself.Employee" table="employee" ...>
...
<set name="employeeDailyRecords" table="employee_daily_record" inverse="{true or false}" fetch="select">
<key>
<column name="EMPLOYEE_ID" not-null="true" >
...
<one-to-many class="com.tuturself.EmployeeRecord" >
...
File : EmployeeDailyRecord.hbm.xml

<hibernate-mapping>
<class name="com.tuturself.EmployeeRecord" table="employee_daily_record" ...>
...
<many-to-one name="employee" class="com.tuturself.Employee">
<column name="EMPLOYEE_ID" not-null="true" >
...
...

inverse=”true”

If inverse='true' in the set variable, it means employee_daily_record is the relationship owner, so Employee will NOT UPDATE the relationship.

<class name="com.tuturself.Employee" table="employee" ...>
...
<set name="employeeDailyRecords" table="employee_daily_record" inverse="true" fetch="select">


inverse=”false”

If inverse='false' (default) in the set variable, it means employee is the relationship owner, and Employee will UPDATE the relationship.

<class name="com.tuturself.Employee" table="employee" ...>
...
<set name="employeeDailyRecords" table="employee_daily_record" inverse="false" fetch="select">

hibernate 12

FOLLOW US ON LinkedIn



Explore Tutu'rself