Hibernate Tutorial «Prev  Next»

Mapping POJO with persistence layer using hibernate mapping document

Each persistent class needs to be mapped with its configuration file. Following code represents Hibernate mapping file for AppLabsUser class.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
  <class name="org.applabs.hibernate.quickstart.javadeploy" table="applabsuser">
    <id column="USER_ID" name="id" type="java.lang.Long">
      <generator class="sequence"/>
    </id>
    <property column="USER_NAME" length="255" name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255" name="userPassword" not-null="true" type="java.lang.String"/>
<property column="USER_FIRST_NAME" length="255" name="userFirstName" type="java.lang.String"/>
<property column="USER_LAST_NAME" length="255" name="userLastName" type="java.lang.String"/>
<property column="USER_EMAIL" length="255" name="userEmail" type="java.lang.String"/>
<property column="USER_CREATION_DATE" length="10" name="userCreationDate" type="java.util.Date"/>
<property column="USER_MODIFICATION_DATE" length="10" name="userModificationDate" type="java.util.Date"/>

  </class>
</hibernate-mapping>  

One can also generate Hibernate mapping documents using Hibernate extension toolset. Hibernate mapping documents are straight forward. The <class> element maps a table with corresponding class. The <id> element represents the primary key column, and its associated attribute in the domain object. The <property> elements represent all other attributes available in the domain object.