Hibernate – Assotations/Relationship : Insert Operation In OneToMany Using XML Configuration(OneToMany):
If the data is divided into more than one java classes objects then one java class objects acts as parent and other java class objects acts as child.To add one to many relationship between a parent class objects and a child class objects. The following two steps are required or need to be followed.
1. Add child object to a collection object.
2. Add collection object to the parent object.
For example if we want to add one-to-many relationship between two class objects called vendor and customer then we need to add customer class(child class) objects to the collection and then the collection objects is added to the vendor class(parent class) object.To apply one-to-many relationship between a parent class object into child class objects, programmatically the following two changes are required.
1. In parent pojo class we need a collection property
2. We need to configure collection property in parent hbm or hibernate mapping file.
To apply one-to-many relationship between a parent class object into child class objects, programmatically the following two changes are required.
1. In parent pojo class we need a collection property
2. We need to configure collection property in parent hbm or hibernate mapping file.
A collection property in a parent class can be of type.
1. java.util.Set (or)
2. java.util.list (or)
3. java.utilo.Map
To configure a collection property in hbm file we can use the following tags.
1. java.util.Set --< <set>
2. java.util.list --< <list> (or) <bag> (or) <idbag>
3. java.utilo.Map --< <map>
To apply one-to-many relationship between a parent class object into child class objects, programmatically the following two changes are required.
1. In parent pojo class we need a collection property
2. We need to configure collection property in parent hbm or hibernate mapping file.
A collection property in a parent class can be of type.
1. java.util.Set (or)
2. java.util.list (or)
3. java.utilo.Map
To apply one-to-many relationship between a parent class object into child class objects, programmatically the following two changes are required.
1. In parent pojo class we need a collection property
2. We need to configure collection property in parent hbm or hibernate mapping file.
To configure a collection property in hbm file we can use the following tags.
java.util.Set --< <set> 2. ava.util.list --< <list> (or) <bag> (or) <idbag> 3.java.utilo.Map --< <map>
Example : <hibernate-mapping>
<class name="com.sada.Model.Vendor" table="VENDOR">
---
<set name="customers" cascade="all" lazy="true">
<key column ="VENDORID" />
<one-to-many class="com.sada.Model.Customer"/>
</set>
</class>
</hibernate-mapping>
Example : public class Vendor {
private int vendorId;
private String vendorName;
private java.util.Set customers;
//setters and getters
}
Hibernate Examples Project Github Link : https://github.com/SadaLearningHub1/H...