Hibernate Inverse

Hibernate Inverse

An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping.

In Hibernate, the relationship is maintained only by the “relationship-owner.” The relationship owner is the in-charge of the relation and handles all the tasks.  It is also used to update the relationship. Hence, the inverse attribute defines which side is the relationship owner. 

The inverse attribute is defined in the XML mapping file of the POJO class.

 Syntax of Inverse

The value of an inverse attribute can be true or false:

  1. inverse = “true”
  2. inverse = “false”
  1. inverse = “true”

If inverse= “true” is set in the XML mapping file, the child class will become the relationship owner. Here, an update is done by the child class.

Syntax of inverse= “true”

 
  • inverse = “false”

If inverse= “false” is set in the XML mapping file, the parent class will become the relationship owner. Here, an update is done by the parent class. If the inverse is not defined in the mapping file, the inverse= “false” is used by default.

Syntax of inverse= “false”

 

Example of Inverse

 Let’s understand the concept of inverse with the help of an example. In this example, we are taking one-to-many association mapping. There are two POJO classes, one is the Vendor, and the other is Customers. One Vendor (parent) can have many Customers (child); therefore, we are adding a collection property in the parent class.

Following are the steps to create an example of the inverse:

  1. Create all POJO classes

Vendor.java

import java.util.Set;
 public class Vendor {
  private int vendid;
  private String name;
  private Set children;
  public int getVendid() {
  return vendid;
  } 
  public void setVendid(int vendid) {
  this.vendid = vendid;
  }
  public String getName() {
  return name; 
  }
  public void setName(String name) {
  this.name = name;
  }
  public Set getChildren() {
  return children; 
  }
  public void setChildren(Set children) {
  this.children = children;
  }
  public Vendor(){
  }
  public Vendor(int vendid, String name, Set children) { 
  super();
  this.vendid = vendid;
  this.name = name;
  this.children = children;
  }
  } 

Customers.java

public class Customers {
  private int cid;
  private String cname;
  public int getCid() {
  return cid;
  }
  public void setCid(int cid) { 
  this.cid = cid;
  }
  public String getCname() {
  return cname;
  }
  public void setCname(String cname) { 
  this.cname = cname;
  }
  public Customers(int cid, String cname) {
  super();
  this.cid = cid;
  this.cname = cname; 
  }
  public Customers() {
  }
  } 
  • Create mapping files for all POJO classes

vendor.hbm.xml


 
 
  
   
  
  
  
  
  
   
  
  
  
  
   
  

customers.hbm.xml


 
 
  
  
  
   
  
  
  
  
  
  
  • Create a configuration file

hibernate.cfg.xml


 
 
  
   
  
   
  
  
  • Create the main class that stores the object of POJO class.

App.java

import java.util.HashSet;
 import java.util.Set;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 public class App 
 {
  public static void main( String[] args ) 
  {
  Configuration cfg= new Configuration();
  cfg.configure("hibernate.cfg.xml");
  SessionFactory fact= cfg.buildSessionFactory();
  Session session= fact.openSession();
  session.beginTransaction(); 
  Vendor vendor= new Vendor();
  vendor.setVendid(01);
  vendor.setName("jyotika");
  Customers customer= new Customers();
  customer.setCid(10);
  customer.setCname("Shikha"); 
  Customers c= new Customers();
  c.setCid(16);
  c.setCname("shubham");
  Set s= new HashSet();
  s.add(customer);
  s.add(c); 
  vendor.setChildren(s);
  session.save(vendor);
  session.getTransaction().commit();
  session.close();
  System.out.println("added");
  session.close();
  }
 } 

Output

App.java

Database Tables

customers

Database Tables

vendor

vendor

vendor_customers

vendor_customers

Related Topics

Hibernate save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.