Next Previous Index

Introduction


This document aims to record an explain the different types of association and collection mapping that Hibernate supports and the effects of choosing each one, including how to set up the mappings and the tables created.


We run through each type of relationship, define it for the uninitiated, give an example usage case, and show how to achieve each relationship with Hibernate.


This document does not pretend to be an exhaustive list Hibernate's capabilities but instead aims to explain the most common relationships. It does not try to recommend any particular approach or judge the merit or performance of each one.


Persistence of the First Rank

We will talk about two main types of Java class; First and second-rank persistent classes:


First-rank persistent classes are those with their own mapping class definition that represent Hibernate entities and must always be explicitly persisted.


Second-rank persistent classes are those either enclosed by first-rank classes or otherwise persisted through associations and relationships with a first-rank class.


Introducing Foo and Bar

Throughout this document we will present examples in terms of two first-rank persistent classes Foo and Bar. For each relationship we extend Foo and Bar (and their mappings) to demonstrate how to support it in Hibernate.


Foo

The basic Foo looks like this:


public class Foo
{
    protected int id;

    public Foo()
    {
    }

    public void setId(int id)
    {
    }

    public int getId()
    {
    }
}

Foo's basic mapping looks like this:


<hibernate-mapping>
    <class name="Foo" table="foo">
        <id name="id" column="id" type="int">
            <generator class="assigned"/>
        </id>
    </class>
</hibernate-mapping>

Bar

The basic Bar looks like this:


public class Bar
{
    protected int id;

    public Bar()
    {
    }

    public void setId(int id)
    {
    }

    public int getId()
    {
    }
}

Bar's basic mapping looks like this:


<hibernate-mapping>
    <class name="Bar" table="bar">
        <id name="id" column="id" type="int">
            <generator class="assigned"/>
        </id>
    </class>
</hibernate-mapping>

For both Foo and Bar we are choosing to an assigned id generator. There is nothing special about this choice. We are simply choosing to program our ids manually.



Next Previous Index