Hibernate One to Many Mapping Insert Query Example
One-to-Many: according to database terminology, one row of table related with multiple rows of other table
[or]
According to hibernate, one object of one pojo class related to multiple objects of other pojo
I mean, one [parent] to many [Children], example of one-to-many is some thing category books contains different type of books, one vendor contains lot of customers bla bla.
To achieve one-to-many between two pojo classes in the hibernate, then the following two changes are required
- In the parent pojo class, we need to take a collection property, the collection can be either Set,List,Map (We will see the example on separate collection later)
- In the mapping file of that parent pojo class, we need to configure the collection
I will take this vendor-customer as an example..
Hibernate One-To-Many Insert Query
files required…
- Vendor.java [pojo class]
- Customer.java [pojo class]
- OurLogic.java
- Customer.hbm.xml
- hibernate.cfg.xml
- Vendor.hbm.xml
Vendor.java
123456789101112131415161718192021222324252627282930package str; import java.util.Set; public class Vendor { private int vendorId; private String vendorName;
private Set children;
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; } public Set getChildren() { return children; } public void setChildren(Set children) { this.children = children; } }
Customer.java
12345678910111213141516171819202122232425262728package str; public class Customer { private int customerId; private String customerName;
private int forevenId;
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; } public int getForevenId() { return forevenId; } public void setForevenId(int forevenId) { this.forevenId = forevenId; } }
Customer.hbm.xml
123456789101112131415<?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"/>
<property name="forevenId" column="forevenid" insert="false" />
</class> </hibernate-mapping>
Vendor.hbm.xml
1234567891011121314151617181920<?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"/>
<set name="children" cascade="all" >
<key column="forevenid" />
<one-to-many class="str.Customer" />
</set>
</class> </hibernate-mapping>
See line number 12, this time i used one new attribute cascade=”all” we will see about this attribute later as separate topic.
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162package str; import java.util.HashSet; import java.util.Set; 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();
//parent object
Vendor v =new Vendor(); v.setVendorId(101); v.setVendorName("java4s");
//creating 3 child objects
Customer c1=new Customer(); c1.setCustomerId(504); c1.setCustomerName("customer4"); Customer c2=new Customer(); c2.setCustomerId(505); c2.setCustomerName("customer5"); Customer c3=new Customer(); c3.setCustomerId(506); c3.setCustomerName("customer6");
// adding child objects to set, as we taken 3rd property set in parent
Set s=new HashSet(); s.add(c1); s.add(c2); s.add(c3); v.setChildren(s); Transaction tx = session.beginTransaction(); session.save(v); tx.commit(); session.close(); System.out.println("One To Many is Done..!!"); factory.close(); } }
Notes:
- In Vendor.java, we have taken a property of type Set
- In Customer.java, we have taken one property forevenId of type int to insert Vendor id
Regarding Vendor.hbm.xml
- In this mapping file, we used a collection configuration element (Set), because in pojo class of vendor, we used the collection type as Set, (we can use Map,List too)
- In order to transfer 0perations on parent object to child object we need to add cascade attribute
- By default, cascade value is none, it means even though relationship is exist, the operations we are doing on parent will not transfer to child, i mean to say here operations are insert, delete, update
- In our Vendor.hbm.xml, we used cascade =”all” means all operations at parent object will be transfer to child
- while applying relationships, we need to configure the foreign key column name, by using which the relationship is done
- Inthe mapping file, we need to use <key /> eliment to configure foregin key column name, in this example forevenid is foreign key
- <one-to-many> is child class with which relation been done, in our example str.Customer is the class [str is the package]
post a comment