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:
- inverse = “true”
- inverse = “false”
- 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:
- 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
Database Tables
customers
vendor
vendor_customers