Hibernate One To Many Mapping
Hibernate One To Many Mapping
A One-to-Many mapping can be implemented using a Set java collection that does not contain
any duplicate element. We already have seen how to map Set collection in hibernate, so if you
already learned Set mapping then you are all set to go with one-to-many mapping.
A Set is mapped with a <set> element in the mapping table and initialized with java.util.HashSet.
You can use Set collection in your class when there is no duplicate element required in the
collection.
Further, assume each employee can have one or more certificate associated with him/her. So we
will store certificate related information in a separate table which has following structure:
import java.util.*;
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
Now let us define another POJO class corresponding to CERTIFICATE table so that certificate
objects can be stored and retrieved into the CERTIFICATE table. This class should also implement
both the equals and hashCode methods so that Java can determine whether any two
elements/objects are identical.
public Certificate() {}
public Certificate(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public boolean equals(Object obj) {
if (obj == null) return false;
if (!this.getClass().equals(obj.getClass())) return false;
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator />
</id>
<set name="certificates" cascade="all">
<key column="employee_id"/>
<one-to-many />
</set>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
You should save the mapping document in a file with the format <classname>.hbm.xml. We saved
our mapping document in the file Employee.hbm.xml. You are already familiar with most of the
mapping detail but let us see all the elements of mapping file once again:
The <class> elements are used to define specific mappings from a Java classes to the
database tables. The Java class name is specified using the name attribute of the class
element and the database table name is specified using the table attribute.
The <meta> element is optional element and can be used to create the class description.
The <id> element maps the unique ID attribute in class to the primary key of the database
table. The name attribute of the id element refers to the property in the class and the
column attribute refers to the column in the database table. The type attribute holds the
hibernate mapping type, this mapping types will convert from Java to SQL data type.
The <generator> element within the id element is used to automatically generate the
primary key values. Set the class attribute of the generator element is set to native to let
hibernate pick up either identity, sequence or hilo algorithm to create primary key
depending upon the capabilities of the underlying database.
The <property> element is used to map a Java class property to a column in the database
table. The name attribute of the element refers to the property in the class and the column
attribute refers to the column in the database table. The type attribute holds the hibernate
mapping type, this mapping types will convert from Java to SQL data type.
The <set> element sets the relationship between Certificate and Employee classes. We
used the cascade attribute in the <set> element to tell Hibernate to persist the Certificate
objects at the same time as the Employee objects. The name attribute is set to the defined
Set variable in the parent class, in our case it is certificates. For each set variable, we need
to define a separate set element in the mapping file.
The <key> element is the column in the CERTIFICATE table that holds the foreign key to the
parent object ie. table EMPLOYEE.
The <one-to-many> element indicates that one Employee object relates to many
Certificate objects.
import java.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
You would get following result on the screen, and same time records would be created in
EMPLOYEE and CERTIFICATE tables.
$java ManageEmployee
.......VARIOUS LOG MESSAGES WILL DISPLAY HERE........
If you check your EMPLOYEE and CERTIFICATE tables, they should have following records:
mysql>
Loading [MathJax]/jax/output/HTML-CSS/jax.js