Hibernate - O/R Mappings-One-to-Many Mappings - Hibernate

What is Hibernate One-to-Many Mappings?

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.

Define RDBMS Tables:

Consider a situation where we need to store our employee records in EMPLOYEE table which will have following structure:

There will be one-to-many relationship between EMPLOYEE and CERTIFICATE objects:

Define POJO Classes:

Let us implement our POJO class Employee which will be used to persist the objects related to EMPLOYEE table and having a collection of certificates in Set variable.

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.

Define Hibernate Mapping File:

Let us develop our mapping file which instructs Hibernate how to map the defined classes to the database tables.

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 mapping document is an XML document having <hibernate-mapping> as the root element which contains two <class> elements corresponding to each class.
  • 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.

Create Application Class:

Finally, we will create our application class with the main() method to run the application. We will use this application to save few Employee's records alongwith their certificates and then we will apply CRUD operations on those records.

Compilation and Execution:

Here are the steps to compile and run the above mentioned application. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

  • Create hibernate.cfg.xml configuration file as explained in configuration chapter.
  • Create Employee.hbm.xml mapping file as shown above.
  • Create Employee.java source file as shown above and compile it.
  • Create Certificate.java source file as shown above and compile it.
  • Create ManageEmployee.java source file as shown above and compile it.
  • Execute ManageEmployee binary to run the program.

You would get following result on the screen, and same time records would be created in EMPLOYEE and CERTIFICATE tables.

If you check your EMPLOYEE and CERTIFICATE tables, they should have following records:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Hibernate Topics