2. Hibernate Configuration

A very simple Hibernate configuration mapping the PERSON table to the Person class and the ADDRESS table to the Address class. There is a one-to-many relationship between the PERSON and ADDRESS table. The set element in the Person mapping creates the relationship from the PERSON to the ADDRESS table and stores the Address instances in a java.util.Set. The key element inside the set element indicates ADDRESS.PERSON_ID is the column to match against PERSON.ID to retrieve addresses associated with a person.

Person.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 package="org.springbyexample.orm.hibernate3.bean" default-access="field">

    <class name="Person" table="PERSON">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>

        <property name="firstName" column="FIRST_NAME" />
        <property name="lastName" column="LAST_NAME" />
        <set name="addresses" lazy="false" inverse="false">
            <key column="PERSON_ID"/>
            <one-to-many class="Address"/>
        </set>
        <property name="created" column="CREATED" />
    </class>
    
</hibernate-mapping>
                
            
Address.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 package="org.springbyexample.orm.hibernate3.bean" default-access="field">

    <class name="Address" table="ADDRESS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>

        <property name="address" column="ADDRESS" />
        <property name="city" column="CITY" />
        <property name="state" column="STATE" />
        <property name="zipPostal" column="ZIP_POSTAL" />
        <property name="created" column="CREATED" />
    </class>
    
</hibernate-mapping>