How does one to one relationship work in Hibernate?
Hibernate Mapping One-to-One
Hibernate provides facility of mapping. You can do mapping through hbm.xml or through annotation. here is an example showing one to one relationship using hbm.xml.
We have two table ?
1.Customer(id,name)
2.Invoice(id,another_field)
Steps to cover this example as-
1.put hibernate library in to lib folder in src.
2.Create getter-setter of table.
Customer.java
package net.roseindia.table; public class Customer { private int id; private String custName; private Invoice invoice; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public Invoice getInvoice() { return invoice; } public void setInvoice(Invoice invoice) { this.invoice = invoice; } }
Invoice.java
package net.roseindia.table; public class Invoice { private int id; private String anotherField; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAnotherField() { return anotherField; } public void setAnotherField(String anotherField) { this.anotherField = anotherField; } }
Now map the tables by using hbm.xml.Each table has its own hbm.xml.
Customer.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.roseindia.table.Customer" table="Customer"> <id name="id" type="int" column="Cus_id"> <generator class="native" /> </id> <property name="custName" type="java.lang.String" column="custName" /> <many-to-one name="invoice" class="net.roseindia.table.Invoice" column="id" not-null="true" cascade="all" unique="true" /> </class> </hibernate-mapping>
Invoice.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.roseindia.table.Invoice" table="Invoice"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <property name="anotherField" type="java.lang.String" column="anotherField" /> </class> </hibernate-mapping>
Next step -
3.
hibernate-cfg.xml is configuration file saved in the same folder where the source code of class file is saved. It creates the connection pool and set-up required environment.
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateRelationShip</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create-drop</property> <!-- Mapping files --> <mapping resource="Invoice.hbm.xml" /> <mapping resource="Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
4.
Now create an util class as HibernateUtil.java
package net.roseindia.util;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory = null; static { sessionFactory = new Configuration().configure().buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Continue....
Here is next step- 5 Here is your main class.
package net.roseindia.application; import net.roseindia.table.Customer; import net.roseindia.table.Invoice; import net.roseindia.util.HibernateUtil; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; public class OneToOneRelation { public static void main(String[] args) { // TODO Auto-generated method stub SessionFactory sessFact = null; Session session = null; try { session = HibernateUtil.getSessionFactory().openSession(); // Create Customer Object Customer cust = new Customer(); // /cust.setId(1); cust.setCustName("Ratna"); // Invoice object Invoice inv = new Invoice(); // Set some values in invoice inv.setAnotherField("Ratna Invoice"); cust.setInvoice(inv);// Set invoice object to customer object Transaction tr = session.beginTransaction(); session.save(cust); tr.commit(); System.out.println("Done"); } catch (HibernateException he) { System.out.println(he.getMessage()); } finally { // SessionFactory close session.close(); } } }
Description:
For creating one to one relationship we use many-to-one element.
We can achieve this by making Invoice column unique in the Customer.