HIBERNATE TUTORIAL

HIBERNATE - Hibernate Complete Example

hbm files

User.hbm.xml

<?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="org.applabs.base.User" table="USER">
    <id column="USER_ID" name="id" type="java.lang.Long">
      <generator class="org.hibernate.id.TableHiLoGenerator">
        <param name="table">idgen</param>
<param name="column">NEXT</param>
      </generator>
    </id>
    <property column="USER_NAME" name="userName" type="java.lang.String"/>
<property column="USER_PASSWORD" name="userPassword" type="java.lang.String"/>
<property column="USER_FIRST_NAME" name="userFirstName" type="java.lang.String"/>
<property column="USER_EMAIL" name="userEmail" type="java.lang.String"/>
<property column="USER_LAST_NAME" name="userLastName" type="java.lang.String"/>
<property column="CREATION_DATE" length="4" name="creationDate" type="java.util.Date"/>
<property column="CREATED_BY" name="createdBy" type="java.lang.Double"/>
<property column="MODIFICATION_DATE" length="4" name="modificationDate" type="java.util.Date"/>
<property column="MODIFIED_BY" name="modifiedBy" type="java.lang.Double"/>
<property column="DELETE_DATE" length="4" name="deleteDate" type="java.util.Date"/>
<property column="DELETED_BY" name="deletedBy" type="java.lang.Double"/>
<set name="properties" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="USER_ID" />
<one-to-many class="org.applabs.base.UserProp" />
    </set>
  </class>
</hibernate-mapping>


UserProp.hbm.xml

<?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="org.applabs.base.UserProp" table="USER_PROP">
    <id column="USER_PROP_ID" name="id" type="java.lang.Long">
      <generator class="org.hibernate.id.TableHiLoGenerator">
        <param name="table">idgen</param>
<param name="column">NEXT</param>
      </generator>
    </id>
    <property column="CATEGORY_NAME" name="categoryName" type="java.lang.String"/>
<property column="CATEGORY_DESCRIPTION" name="categoryDescription" type="java.lang.String"/>
<property column="CREATED_BY" name="createdBy" type="java.lang.Double"/>
<property column="CREATION_DATE" length="4" name="creationDate" type="java.util.Date"/>
<property column="MODIFIED_BY" name="modifiedBy" type="java.lang.Double"/>
<property column="MODIFICATION_DATE" length="4" name="modificationDate" type="java.util.Date"/>
<property column="DELETED_BY" name="deletedBy" type="java.lang.Double"/>
<property column="DELETE_DATE" length="4" name="deleteDate" type="java.util.Date"/>
<!- One way of implementing -->
<!-- <property column="PARENT_CATAGORY" name="parentCatagory" type="java.lang.Long"/> -->
<!- Another way of One-to-many Mapping -->
<set name="childCatagories">
      <key column="CATAGORY_ID" not-null="true"/>
<one-to-many class="org.applabs.base.Category"/>
    </set>
    <set name="properties" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="CATEGORY_ID" />
<one-to-many class="org.applabs.base.CategoryProp" />
    </set>
  </class>
</hibernate-mapping>


Category.hbm.xml

<?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="org.applabs.base.Category" table="CATEGORY">
    <id column="CATAGORY_ID" name="id" type="java.lang.Long">
      <generator class="org.hibernate.id.TableHiLoGenerator">
        <param name="table">idgen</param>
<param name="column">NEXT</param>
      </generator>
    </id>
    <property column="CATEGORY_NAME" name="categoryName" type="java.lang.String"/>
<property column="CATEGORY_DESCRIPTION" name="categoryDescription" type="java.lang.String"/>
<property column="CREATED_BY" name="createdBy" type="java.lang.Double"/>
<property column="CREATION_DATE" length="4" name="creationDate" type="java.util.Date"/>
<property column="MODIFIED_BY" name="modifiedBy" type="java.lang.Double"/>
<property column="MODIFICATION_DATE" length="4" name="modificationDate" type="java.util.Date"/>
<property column="DELETED_BY" name="deletedBy" type="java.lang.Double"/>
<property column="DELETE_DATE" length="4" name="deleteDate" type="java.util.Date"/>
<!- One way of implementing -->
<!-- <property column="PARENT_CATAGORY" name="parentCatagory" type="java.lang.Long"/> -->
<!- Another way of One-to-many Mapping -->
<set name="childCatagories">
      <key column="CATAGORY_ID" not-null="true"/>
<one-to-many class="org.applabs.base.Category"/>
    </set>
    <set name="properties" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="CATEGORY_ID" />
<one-to-many class="org.applabs.base.CategoryProp" />
    </set>
  </class>
</hibernate-mapping>


CategoryProp.hbm.xml

<?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="org.applabs.base.CategoryProp" table="CATEGORY_PROP">
    <id column="CATEGORY_PROP_ID" name="id" type="java.lang.Long">
      <generator class="org.hibernate.id.TableHiLoGenerator">
        <param name="table">idgen</param>
<param name="column">NEXT</param>
      </generator>
    </id>
    <property column="CATEGORY_ID" name="categoryId" type="java.lang.Long"/>
<property column="PROP_NAME" name="propName" type="java.lang.String"/>
<property column="PROP_VALUE" name="propValue" type="java.lang.String"/>
    </set>
  </class>
</hibernate-mapping>


Document.hbm.xml

<?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="org.applabs.base.Document" table="DOCUMENT">
    <id column="DOCUMENT_ID" name="id" type="java.lang.Long">
      <generator class="sequence"/>
    </id>
    <property column="DOCUMENT_TITLE" name="documentTitle" type="java.lang.String"/>
<property column="DOCUMENT_BODY" name="documentBody" type="java.lang.String"/>
<property column="DOCUMENT_SUMMARY" name="documentSummary" type="java.lang.String"/>
<property column="CREATED_BY" length="65535" name="createdBy" type="java.lang.Double"/>
<property column="CREATION_DATE" length="4" name="creationDate" type="java.util.Date"/>
<property column="MODIFIED_BY" length="65535" name="modifiedBy" type="java.lang.Double"/>
<property column="MODIFICATION_DATE" length="4" name="modificationDate" type="java.util.Date"/>
<property column="DELETED_BY" length="65535" name="deletedBy" type="java.lang.Double"/>
<property column="DELETED_DATE" length="4" name="deletedDate" type="java.util.Date"/>
<property column="DOCUMENT_VERSION" length="65535" name="documentVersion" type="java.lang.Double"/>
<property column="DOCUMENT_STATUS" length="65535" name="documentStatus" type="java.lang.Double"/>
<property column="ENTRY_ID" length="65535" name="entryId" type="java.lang.Double"/>
  </class>
</hibernate-mapping>


DocumentProp.hbm.xml


<?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="org.applabs.base.DocumentProp" table="DOCUMENT_PROP">
    <id column="document_prop_id" name="id" type="java.lang.Long">
      <generator class="sequence"/>
    </id>
    <property column="document_id" length="65535" name="documentId" not-null="true" type="java.lang.Double"/>
<property column="prop_name" name="propName" not-null="true" type="java.lang.String"/>
<property column="prop_value" name="propValue" type="java.lang.String"/>
<property column="prop_value_details" name="propValueDetails" type="java.lang.String"/>
  </class>
</hibernate-mapping>


 

Hibernate tutorial
Introduction to Hibernate
Overview of Hibernate
Features of Hibernate
Getting Started with Hibernate
Hibernate O/R Mapping
Hibernate Mapping In Depth
Hibernate Query Language
Hibernate Complete Example

Introduction

Database scheme
Basic entities
Categories and Documents
Database Scripts
Database tables Overview
hbm files
Complete Code Download

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions