Hibernate Many to One Mapping Insert Query Example
Let us see how to achieve hibernate many to one mapping with insert query, just go through few points before we start the example
- In the many to one relationship, the relationship is applied from child object to parent object, but in one to may parent object to child object right..! just remember
- many to one is similar to one to many but with the little changes
- If we want to apply many to one relationship between two pojo class objects then the following changes are required
In the child pojo class, create an additional property of type parent for storing the parent object in child object [ If you are confused just remember you will be able to understand in the example ], in the child pojo class mapping file we need to write <many-to-one name=””> for parent type property unlike <property name=””>
Example on Many to One With Insert Query
files required…
- Vendor [parent pojo]
- Customer.java [child pojo]
- OurLogic.java [our logic]
- Vendor.hbm.xml
- Customer.hbm.xml
- hibernate.cfg.xml
Vendor.java
123456789101112131415161718192021package str; public class Vendor { private int vendorId; private String vendorName; public int getVendorId() { return vendorId; } public void setVendorId(int vendorId) { this.vendorId = vendorId; } public String getVendorName() { return vendorName; } public void setVendorName(String vendorName) { this.vendorName = vendorName; } }
Customer.java
1234567891011121314151617181920212223242526package str; public class Customer { private int customerId; private String customerName; private Vendor parentObjects; public Vendor getParentObjects() { return parentObjects; } public void setParentObjects(Vendor parentObjects) { this.parentObjects = parentObjects; } public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } }
Vendor.hbm.xml
12345678910111213<?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="str.Vendor" table="vendor"> <id name="vendorId" column="vendid" /> <property name="vendorName" column="vendname" length="10"/> </class> </hibernate-mapping>
Customer.hbm.xml
1234567891011121314<?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="str.Customer" table="customer"> <id name="customerId" column="custid" /> <property name="customerName" column="custname" length="10"/>
<many-to-one name="parentObjects" column="Vdummy" class="str.Vendor" cascade="all" />
</class> </hibernate-mapping>
hibernate.cfg.xml
123456789101112131415161718192021<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Customer.hbm.xml"></mapping> <mapping resource="Vendor.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
OurLogic.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354package str; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class OurLogic { public static void main(String args[]) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Vendor v =new Vendor(); v.setVendorId(101); v.setVendorName("java4s"); Customer c1=new Customer(); c1.setCustomerId(504); c1.setCustomerName("customer4");
c1.setParentObjects(v);
Customer c2=new Customer(); c2.setCustomerId(505); c2.setCustomerName("customer5");
c2.setParentObjects(v);
Customer c3=new Customer(); c3.setCustomerId(506); c3.setCustomerName("customer6");
c3.setParentObjects(v);
Transaction tx = session.beginTransaction();
session.save(c1);
session.save(c2);
session.save(c3);
tx.commit(); session.close(); System.out.println("One To Many is Done..!!"); factory.close(); } }
Notes:
- In line numbers 28,34,40 we are adding parent to child object
- In line number 44,45,46 we are saving all child objects, but you know some thing, according to this code at line number 45, first child object will be saved with the parent, at 45,46 just child (customer object) only will be saved as parent is saved already
post a comment