Generally, the configuration provides the following information:
The mapping between the POJO and the database table
The mapping between the POJO property and the database table column
The definition of the primary key column
The definitions of the foreign key column and relationships such as one-to-one, one-to-many, many-to-one, many-to-many with another table, and so on
Constraints such as not-null, formula, lazy, cascade, and so on
The definitions of the length, the data type of the column, the formula, and so on
To provide hibernate mapping based on XML, perform the following steps:
Ensure that the basic structure of the configuration file is as follows:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD X.X//EN" "http://hibernate.sourceforge.net/hibernate-mapping-X.X.dtd"> <hibernate-mapping> ... </hibernate-mapping>
<hibernate-mapping> <class="Employee" table="employee"> <id name="id" type="long" column="id"> <generator class="increment" /> </id> <property column="firstName" name="firstName" /> <property column="salary" name="salary" /> <!-- other properties mapping--> </class> </hibernate-mapping>
Here, we named the mapping file
Employee.hbm.xml. However, while developing, there is no strict rule regarding the naming convention. Here, we will create a new hbm file for each POJO; for example, we created an
Employee.hbm.xmlfile for the
Employee.javaclass. Another common practice we will use here is to create one hbm file for the module, map all the classes of this module in the same mapping file, and name this file
<hibernate-mapping> is a root element that contains all the
<class> elements. The
<class> tag contains the following attributes:
name: This specifies the FQN (Fully Qualified Name) of the Java class.
table:This denotes the database table used for the class defined in the
<generator>tag in the
<id>tag is used to generate the value of the primary key. There are many types of built-in generators provided by hibernate, such as
hilo, and so on.
<id> tag defines the primary key column for the database table. It contains the following attributes:
name: This specifies the Java class attribute name
column: This denotes the database table's column name
type: This specifies the data type of the column that will help hibernate during the creation and retrieval of the automatic table
size: This denotes the size attribute that defines the length of the table's column
Usually, we create a mapping file called hbm (hibernate mapping). It is a normal XML schema file that contains custom hibernate XML tags. This helps the hibernate engine to map the class to the table and the
class field to the
table column, along with the given attributes.
All the mapping definitions for hibernate are bundled under the
<hibernate-mapping> ... </hibernate-mapping> tag. In
<hibernate-mapping> ... </hibernate-mapping>, we can add any number of class-to-table mapping definitions.
Now, let's create the XML mapping for the POJO having a reference with another POJO. Here, we will create two different mapping files. To achieve this using an XML-based mapping, we have to create different class mappings for each POJO that has a dependency.
… <hibernate-mapping> <class name="Department" table="department"> <id name="id" type="long" column="id"> <generator class="auto" /> </id> <property column="deptName" name="deptName" /> <!-- other properties mapping --> </class> </hibernate-mapping> …
… <hibernate-mapping> <class="Employee" table="employee"> <id name="id" type="long" column="id"> <generator class="auto" /> </id> <property column="firstName" name="firstName" /> <property column="salary" name="salary" /> <many-to-one name="department" class="Department" > <column name="department"/> </many-to-one> <!-- other properties mapping--> </class> </hibernate-mapping> …
In the preceding example, we mapped the
Department entity with the
Employee entity. This will refer to the
department column in the employee table. This means that it will create a foreign key that is referenced to the department table.
Here, we will use the
<many-to-one> relationship, which means that either many employees are connected with one department, or one department is used by many employees.
The properties are as follows:
true": This property prevents the user from inserting the NULL value in the column
true": This feature helps us while retrieving data using hibernate
The two possible options for
false. In our example,
Employee is a parent class, whereas
Department is a child of the
Employee class. Now, while fetching, if we set
true, it means that it will only fetch employee records. No child records will be fetched with
Employee, and hibernate will use a separate query if we try to access a child record, which is
employee.getDepartment(). If we set
false, hibernate will fetch the child records with the parent, which means that the department information will also be fetched, along with that of the employee. Hibernate will use a join query to fetch the child records.