Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Hibernate inverse keyword

In this section, you will learn how to use inverse keyword in Hibernate.

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

Hibernate inverse keyword

In this section, you will learn how to use inverse keyword in Hibernate.

The keyword inverse is always very confusing to the developers. The inverse keyword is always used in One-to-Many and Many-to-Many relationship, Many-to-One doesn't have this keyword. It tells us which side is the relationship owner. In a relation (One-to-Many / Many-to-Many) , relationship owner is in charge of the relationship and handle the relationship among the two.

The keyword  inverse=true means the related table is relationship owner and inverse=false means table is not the relationship owner.

The inverse  keyword tells Hibernate which table is the relationship owner between the two tables and the class related to the relationship owner table will UPDATE the relationship.

Consider the below example, the employee and address table has a one-to-many relationship.

The employee table has many-to-one relationship. And the address table has one-to-many relationship.

 The Address.hbm.xml is given below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Address" table="address">
<id name="addressId" column="address_id">
<generator class="native" />
</id>
<property name="street" column="street"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="country" column="country"/>
<set name="employees" table="employee" inverse="{true/false}" lazy="true" fetch="select">
<key>
<column name="address_id" not-null="true" />
</key>
<one-to-many class="net.roseindia.Employee" />
</set>
</class>
</hibernate-mapping>

inverse = true

Here inverse can be true or false. Here, if inverse=true , employee is the relationship owner. So, Address will not UPDATE the relationship

inverse = false

If inverse=false , address is the relationship owner and Address will UPDATE the relationship.

If keyword inverse is not define

If keyword inverse is not define,  inverse=false is used as default.

Example

When inverse=false in the above, the following code will produce three SQL statements, two inserts and one update :

session.beginTransaction();

Address address = new Address();
address.setStreet("Lake Gardens");
address.setCity("Kolkata");
address.setState("West Bengal");
address.setCountry("India");


Employee e1 = new Employee("Rakesh", "Sharma", "9999999999");

e1.setAddress(address);
address.getEmployees().add(e1);

session.save(address);
session.save(e1);

session.getTransaction().commit();

Output

Hibernate: insert into address (street, city, state, country) values (?, ?, ?, ?)
Hibernate: insert into employee (firstname, lastname, cell_phone, address_id) values (?, ?, ?, ?)
Hibernate: update employee set address_id=? where employee_id=?

Address will update the ?employee.address_id? through Set variable (employees), because Address is the relationship owner.

Advertisements

Share on Google+Share on Google+

Hibernate inverse keyword

Posted on: March 20, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate inverse keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads