Friday, September 04, 2009

Hibernate Getting Started

Source: Hibernate Reference documentation

Hibernate Getting Started


Hibernate Mapping File - Specifies how instances of persistent classes are to be persisted and loaded. This specification is laid out by detailing the properties that are mapped to columns, classes that are mapped to tables and the relationship between classes mapped in DB semantics like foreign keys (and join tables?).

Basic Hibernate Mapping File (there are multiple listings of the same file. Each listing builds upon the previous listing):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <!-- This file will first be looked up in classpath, then on the web. -->
<hibernate-mapping package="org.hibernate.tutorial.domain">
[...]
</hibernate-mapping>


<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <!-- All classes that represent persistent entities, must be mapped to a table -->
    <!-- This tells hibernate to persist and load instances of Event class to the EVENTS table. Each instance of Event is a row in EVENTS table -->
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
 
    <!--
The class Event bean has a property (or getter and setter) by name "id" that serves as a key or id of Event class. The id has to be stored to the column EVENT_ID. -->
      <generator class="native"/>
        <!-- Use hibernate's key generation strategy (How are identifier values generated?) to generate the Keys for Events. Other strategies include Data base generated keys, Globally unique keys, application generated keys -->
    </id>
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
      <generator class="native"/>
    </id>
    <!-- Other properties of Event class. If a property is not mapped, it is not considered persistent. -->
    <property name="date" type="timestamp" column="EVENT_DATE"/>
      <!-- Event has a getDate() and setDate() and the value is to be stored in the column named EVENT_DATE. Type is hibernate mapping type. There are converters that convert between Java to SQL data types and visa-versa. If type not specified, hibernate will try to determine the mapping type and conversion type. This automatically (detected through reflection - can impact start up performance) type may not be what you want. For example java.util.Date can map to "date" (only date e.g. July 4th, 2009), "timestamp" (both date and time e.g. July 4th, 2009 10:00:00 HRS) or "time" (only time e.g. 10:00:00 HRS). Then you explictly specify the type -->
    <property name="title"/>
      <!-- Event has a getTitle() and setTitle(). Column name is not specified, so use the property name as the column name. -->
  </class>
</hibernate-mapping>


No comments: