Generic Hibernate Application Requirements – XML Mapping
The objective of this example is to understand the general requirement to be followed in creating any hibernate application in Java. You may want to look at Hibernate Installation/Setup on Eclipse IDE article if Hibernate is not installed already on your system.
In general any hibernate application, must have the following 4 files,
Model class Mapping XML Configuration XML One java file to access this configuration file/write our logic
These files are the minimum requirement to run an hibernate application, in case of complex application we may require many Model classes and many mapping xml files, one configuration xml and a java program to access the configuration details and to write our logic.
Note : Number of Model classes = Number of mapping xml files
Model class
Model class is simple POJO (Plane old java object), that does extend any class or implement any interface, for each member variable defined in this class you must create a getter and a setter.
Example:Student.java
package model; public class Student { private int id; private String name; private String department; private String college; // Create Getters and Setters }
Hibernate Mapping file for Model Class
The mapping file is where the Hibernate mapping comes into play, this mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.
Student.hbm.xml : A simple hibernate XML mapping
As shown above, the mapping file contains several elements, listed below are its role:
- Class element takes care of mapping Student class from java side to STUDENT table in the database
- Id element, indicates which column in database table we need to take as primary key column.
In the propertyname
of “id
” element we need to give the member variable name from the student class(id
) which will mapped withSTUDENT_ID
column in the STUDENT table.
In the above example “id
” member variable of Student class is mapped withSTUDENT_ID
Column of database, and thisSTUDENT_ID
is the primary key of the table STUDENT.
Note:
I will explain about this<generator />
element later.
- Property element, used from non-primary key mapping , in this example name member variable is mapped to “STUDENT_NAME” of STUDENT table.
Question: Why doesid
andname
property mapping include thecolumn
attribute, but thedepartment
andcollege
does not?
Answer: Without thecolumn
attribute, Hibernate by default uses the property name as the column name. This works fordepartment
andcollege
, however if we have a member variable calleddate
in java class, now this date is a reserved keyword in most of the database, so you will need to map it to a different name, so in such scenarios we can go forcolumn
attribute in xml mapping.
Hibernate Configuration file
In this mapping file Hibernate gets to know about the type of database and its connection properties as well as about the mapping files.
File: hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver system admin jdbc:oracle:thin:@127.0.0.1:1521:XE org.hibernate.dialect.Oracle10gDialect true update
In our next article we shall learn about Generic Steps to be followed to use Hibernate in any Java application, going forward using this two articles we shall implement Hello World example of Hibernate 4 in Eclipse