May 17, 2021 Hibernate
An object/relationship map is generally defined in an XML file. T he mapping file indicates how Hibernate corresponds a defined class or group of classes to a table in the database.
Although some Hibernate users choose to write XML files by hand, there are many tools you can use to generate mapping files for advanced Hibernate users. S uch tools include XDoclet, Middlegen, and AndroMDA.
Let's consider our previously defined POJO class, where the objects are extended to the table defined in the next section.
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
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;
}
}
For every object you want to provide persistence, you need a table to match it. C onsider that the above objects need to be stored and retrieved into the following RDBMS tables:
create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);
Based on these two entities, we can define the following mapping files to indicate how Hibernate matches defined classes or groups of classes to database tables.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<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 class="native"/>
</id>
<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'll need to save the mapping file in the format
<classname>.hbm.xml
W
e save the mapping file in the Employee.hbm .xml file. L
et's take a look at some of the labels used in the mapping file in detail:
<hibernate-mapping>
which
<class>
<class>
a specific map from a Java class to a database table.
The class name of Java is
represented by the name
property, and the database is
represented by
the table property.
<meta>
can be used to decorate classes.
<id>
ID property in the class with the primary key in the database table. T
he name property in the
id element
refers to the nature of the
class, and the column
property refers to the column of the database table name.
The type
property holds the type of Hibernate map, which is converted from Java to SQL data type.
<generator>
generate primary key values.
Setting the class property
in the generator label
sets the native so
that
Hibernate can use
the identity,
sequence,
or hilo algorithm
to create primary keys based on the underlying database.
<property>
of the Java class to the columns of the database table. T
he name
property in
the label refers to the nature of the class, and the
column
property refers to the columns of the database table.
The type
property holds the type of Hibernate map, which is converted from Java to SQL data type.
There are other properties and elements available in the mapping file, and I'll cover as much as possible in other topics related to Hibernate.