About Hibernate inverse=”true” or inverse=”false” attribute

Java 

Parent.java

package javafiles;

 

import java.util.Set;

 

publicclass Parent {

privateintid;

private String name;

private Set children;

publicint getId() {

returnid;

}

publicvoid setId(int id) {

this.id = id;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

public Set getChildren() {

returnchildren;

}

publicvoid setChildren(Set children) {

this.children = children;

}

}

 

Children.java

package javafiles;

 

publicclass Children {

privateintid;

private String name;

privateintage;

privateintparentId;

private Parent parent;

publicint getId() {

returnid;

}

publicvoid setId(int id) {

this.id = id;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

publicint getAge() {

returnage;

}

publicvoid setAge(int age) {

this.age = age;

}

publicint getParentId() {

returnparentId;

}

publicvoid setParentId(int parentId) {

this.parentId = parentId;

}

 

public Parent getParent() {

returnparent;

}

 

publicvoid setParent(Parent parent) {

this.parent = parent;

}

}

 

Inverse=”false”

Xml

Parent.hbm.xml File

<class name=“javafiles.Parent” table=“PARENTS_DATA”>

<id name=“id”  column=“ID” >

<generator class=“increment”/>

</id>

<property name=“name” column=“NAME” length=“20”/>

<set name=“children” cascade=“all” inverse=“false”>

<key column=“PARENT_ID”/>

<one-to-many class=“javafiles.Children”/>

</set>

</class>


Children.hbm.xml file

<class name=“javafiles.Children” table=“CHILDREN_DATA”>

<id name=“id” column=“ID”>

<generator class=“increment”/>

</id>

<property name=“name”            column=“NAME”       />

<property name=“age”             column=“AGE”        />

<many-to-one name=“parent” class=“javafiles.Parent”>

<column name=“PARENT_ID” />

</many-to-one>

</class>

Insert/Save

SessionFactory factory = cfg.buildSessionFactory();

Session session = factory.openSession();

Parent parentObj=new Parent();

parentObj.setName(“Raj”);

Children childObj1=new Children();

childObj1.setName(“Krishna”);

childObj1.setAge(56);

Children childObj2=new Children();

childObj2.setName(“Rajvir”);

childObj2.setAge(29);

Set<Children> childSet=new HashSet<Children>();

childSet.add(childObj1);

childSet.add(childObj2);

parentObj.setChildren(childSet);

Transaction transaction = session.beginTransaction();

session.save(parentObj);

 

Just set the child object into parent object and perform save operation on parent object. It will perform the operation successfully.

Must Read

But here it will perform additional update operation query on the child object’s table. Number of update operation query will be same as the number of child objects added into parent object.

Delete

Parent parentObj= (Parent) session.get(Parent.class, new Integer(1));

Transaction transaction=session.beginTransaction();

session.delete(parentObj);

 

Must Read

Delete the parent object using the session.delete() method it will delete the related child objects also. In delete operation also it will fire one update operation query on child object table.

 

Inverse=”true”

Xml

Parent.hbm.xml File

<class name=“javafiles.Parent” table=“PARENTS_DATA”>

<id name=“id”  column=“ID” >

<generator class=“increment”/>

</id>

<property name=“name” column=“NAME” length=“20”/>

<set name=“children” cascade=“all” inverse=“true”>

<key column=“PARENT_ID”/>

<one-to-many class=“javafiles.Children”/>

</set>

</class>


Children.hbm.xml file

<class name=“javafiles.Children” table=“CHILDREN_DATA”>

<id name=“id” column=“ID”>

<generator class=“increment”/>

</id>

<property name=“name”            column=“NAME”       />

<property name=“age”             column=“AGE”        />

<many-to-one name=“parent” class=“javafiles.Parent”>

<column name=“PARENT_ID” />

</many-to-one>

</class>

Must Read

Same as previous code Set the child object into parent object and perform save operation on parent object. It will save the data into both parent object’s and child object’s table. But it will not save the PARENT_ID into child’s object table. It will remain null.

Same way perform delete operation it will delete the data from the parent object’s table but it will not delete the data from the child object’s table.

The reason is inverse=”true”

 

<set name=“children” cascade=“all” inverse=“true” >

<key column=“PARENT_ID”/>

<one-to-many class=“javafiles.Children”/>

</set>

It indicates that the owner of the PARENT_ID column value is name=“children”  property of parent object. Unless we will set the parent object into child object it will not perform any operation on the PARENT_ID column.

 

Insert/Save

 

Parent parentObj=new Parent();

parentObj.setName(“Raj”);

Children childObj1=new Children();

childObj1.setName(“Krishna”);

childObj1.setAge(56);

        childObj1.setParent(parentObj);

Children childObj2=new Children();

childObj2.setName(“Rajvir”);

childObj2.setAge(29);

childObj2.setParent(parentObj);

Set<Children> childSet=new HashSet<Children>();

childSet.add(childObj1);

childSet.add(childObj2);

        parentObj.setChildren(childSet);

Transaction transaction = session.beginTransaction();

session.save(parentObj);

 

Delete

        session.delete(parentObj);

 

If we will set the parent object into child object

then only in save operation it will save the PARENT_ID into child object’s table.

And in delete operation it will delete the child data from the child object’s table.

Here it will not perform additional Update operation on child object’s table as in the case of inverse=”false”

 

The inverse=”true” gives the ownership  of  particular column  value to the respected object. By default it value is inverse=”false” and it gives ownership to the parent object.

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in Hibernate. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s