How to specify mapping type in Hibernate Mapping file

As known there are two types of XML files in any Application, which is using Hibernate. These two XML files are
1. Configuration File, generally named as hibernate.cfg.xml
2. Mapping File, can be given any name

Note that, Hibernate framework reads contents of above files, to
1. to establish connection to database
2. to create/update tables and columns
3. to select/insert/update records in tables.

Below is an example of hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
      <property name="hbm2ddl.auto">create</property> 
      <property name="show_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zzzz?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">abcdfe</property>
    <mapping resource="hibernate.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

As seen above hibernate.cfg.xml file specifies below details
1. Database details
2. JDBC Driver details
3. Database credentials(i..e username and password)
4. list of hibernate mapping files
5. Any other optional property such as below
hbm2ddl.auto – to automatically create or update database tables or columns
show_sql – shows sql queries generated by Hibernate framework. These sql queries are executed in database. This option is generally used for debugging purpose, when Application is in development.

Below is an example of .hbm.xml file

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="Trainer" table="ttrainer">
      <meta attribute="class-description">
         This class contains the Trainer details. 
      </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"/>
      <many-to-one name="student" column="trn_student" unique="true" 
       class="Student" not-null="true"/>
   </class>

   <class name="Student" table="tstudent">
      <meta attribute="class-description">
         This class contains Student details. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="sName" column="stdnt_name" type="string"  not-null="true"/>
      <property name="sPhone" column="stdnt_phone" type="string"/>
   </class>

</hibernate-mapping>

As seen above .hbm.xml file specifies below details
1. Mapping between class, and database table
2. Mapping between data members (in each class) and table columns, along with data type
3. Type of relationship between Trainer and Student classes.

Note that he data type specified above, is neither Java type nor SQL Type. It is Hibernate Mapping type. Below shows Hibernate Mapping Type for corresponding Java Type and SQL Type.

hibernate-mapping-types
hibernate-mapping-types

You may also like to read: