Hibernate Mapping and Configuration Files

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Hibernate Configuration File (hibernate.cfg.xml)

Hibernate needs a set of configuration settings related to database and all these information related to database you need to set in the hibernate.properties, or an XML file named hibernate.cfg.xml. These are the important configuration files and you need to configure everything in these files, like the database url, username, password, mapping information and so on. Most of the properties take their default values and no need to change as it is not required to specify them in the property file unless it is required. You have to place these file in the classpath of your application or in the applications root directory as you want.

Note: If both the hibernate.properties and hibernate.cfg.xml files are found in the application classpath, then hibernate.cfg.xml overrides all the settings which is found in the hibernate.properties file.

Example: hibernate.cfg.xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	<property name="hibernate.connection.driver_class">
	<property name="hibernate.connection.password">
	<property name="hibernate.connection.url">
	<property name="hibernate.connection.username">
	<property name="hibernate.dialect">
	<property name="show_sql">
	<mapping resource="com/javatutsworld/do/Employee.hbm.xml">

The configuration file requires the following properties:

  • hibernate.connection.driver_class: -The JDBC connection class for the specific database
  • hibernate.connection.url: - Full JDBC URL to the database
  • hibernate.connection.username : - Username used to connect to the database
  • hibernate.connection.password : - Password used to authenticate the username
  • hibernate.dialect: - The name of the SQL dialect for the database
  • hibernate.connection.pool_size:- Number of connections limit for waiting in the Hibernate database connection pool.

Hibernate Mapping File (Employee.hbm.xml)

Hibernate mapping files are used to specify how your objects relate to database tables. The mapping files also provide support features, such as creating the database schema from a collection of mapping files. All Mapping for persistent objects may be stored together in a single mapping file or for each object can be stored in an individual mapping file. The last approach is best approach. For storing the definitions for a large number of persistent classes in one mapping file can create confusion so you should use the file-per-class method to organize your mapping documents properly. Hibernate needs this mapping placing an object properties into column s of a table. Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations. The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.

Actually mapping can be done using 2 ways,

  • XML
  • Annotations

Example: The Employee.hbm.xml mapping file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"

   <class name="Employee" table="EMPLOYEE"> 
   <id name="id" type="int" column="id">
         <generator class="native"/>
   <property name="firstName" column="first_name" type="string"/>
   <property name="lastName" column="last_name" type="string"/>  
  • The mapping document having hibernate-mapping as the root element which contains all the class elements.
  • The class elements are used to define specific mappings from a Java classes to the database tables. Class name is specified using the name attribute and the database table name is specified using the table attribute.
  • 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 will convert from Java data type to SQL data type.
  • The generator element is used to automatically generate the primary key values.
  • The property element is used to map a Java class property to a column name in the database table. The type attribute holds the hibernate mapping type and it will convert from Java data type to SQL data type.