2

According to nHibernate profiler, I need to add inverse="true" to my mapping file, however, I can't seem to find examples on where exactly to put this property. Can anyone tell me based on the following mapping file where I need to put inverse="true"?

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="LROLib.Domain" assembly="LROLib"> <class name="TestResult" table ="Test_Results" > <id name="Test_Result_Id" > <generator class="native" /> </id> <many-to-one name="Test_Result" class="Result" column="Result_Id" /> <many-to-one name="Test_Applicant" class="Applicant" column="Applicant_Id"/> <property name="Test_Name" /> <property name="Value" /> <property name="Hi_Lo_Ind" /> <property name="Range" /> <property name="Unit_Of_Measure" /> <property name="Lo_Range" /> <property name="Hi_Range" /> <property name="Create_DateTime" update="false"/> <property name="Update_DateTime" /> <property name="User_Name" /> </class> </hibernate-mapping> 
0

3 Answers 3

5

In most cases 'inverse' is to identify the relationship owner. For your particular case this should be in your Result and Applicant mappings.

You can read more in this blog post. To quote / paraphrase a particularly relevant statement from that article:

However the “inverse” keyword itself is not verbose enough, I would suggest change the [inverse] keyword to "relationship_owner".

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mynkow, I realized I was looking in the wrong mapping file. I went to my Applicant mapping file and updated the following section: <set name="TestResults" order-by="Test_Result_Id asc" inverse="true"> <key column="Applicant_Id"/> <one-to-many class="TestResult"/> </set>
1

When I was a hibernate beginner, I was confused about deciding "inverse=true" or "inverse=false".

Here is an easy way to understanding it:

example scenario: Person(one) <-> Address(many) bi-directional one-to-many relationship. (A person has multiple addresses.)

public class Person { private Integer id; private Set<Address> addresses; // setter, getter Set<Address> getAddresses() { return addresses; } .... }

public class Address { private Integer id; private Person person; // setter, getter Person getPerson() { return person; } ..... }

  • Person class has "Set getAddresses()" method
  • Address class has "Person getPerson()" method

If you think about the relation between two classes, it may gives an idea that person has addresses. (Person -> Addresses) So, it feels like a person is an owner, and an address is a child. Then, you want to think that address is "inverse=true" because address is owned by person. However, it's not correct.

Here, I'd like to suggest a way to think about it. Let's look at table structure instead of classes.

  • PERSON[ id, name, ...]
  • ADDRESS[ id, person_id, city, street,...]

The person_id column in Address table is the relational information between thease two tables. So, Address is an owner of the relationship, and Person is the inverse side. "inverse=true" means "this side is the inverse side", and "inverse=false" means "this is not the inverse side. this side is the owner of the relationship".

<class name="Person"> <id name="id">...</id> <set name="addresses" inverse="true"> <key column="person_id"/> <one-to-many class="Address"/> </set> </class> <class name="Address"> <id name="id">...</id> <many-to-one name="person" class="Person" /> </class>

In sum, look at the table structure to distinguish "inverse=true" or "inverse=false". If the table has relational information, then it is the owner side. So, it's not inverse side.(inverse=false) If the table doesn't have relational information, then it is the inverse side. So, it needs "inverse=true".

Comments

0

You can find a well explained tutorial in: What is “inverse” by mkyong.

It focuses on what @mynkow states and give you some examples for inserting and updating regarding the value of inverse attribute.

If I were you, I would give it a look.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.