2005-01-30 07:54:29 -05:00
|
|
|
<chapter id="persistent-classes" revision="2">
|
2004-06-03 12:31:32 -04:00
|
|
|
<title>Persistent Classes</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Persistent classes are classes in an application that implement the entities
|
|
|
|
of the business problem (e.g. Customer and Order in an E-commerce application).
|
2005-01-30 07:54:29 -05:00
|
|
|
Not all instances of a persistent class are considered to be in the persistent
|
|
|
|
state - an instance may instead be transient or detached.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Hibernate works best if these classes follow some simple rules, also known
|
2005-06-05 03:28:53 -04:00
|
|
|
as the Plain Old Java Object (POJO) programming model. However, none of these
|
2005-01-30 07:54:29 -05:00
|
|
|
rules are hard requirements. Indeed, Hibernate3 assumes very little about
|
|
|
|
the nature of your persistent objects. You may express a domain model in other
|
|
|
|
ways: using trees of <literal>Map</literal> instances, for example.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect1 id="persistent-classes-pojo">
|
|
|
|
<title>A simple POJO example</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Most Java applications require a persistent class representing felines.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[package eg;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
public class Cat {
|
|
|
|
private Long id; // identifier
|
2004-08-20 09:44:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
private Date birthdate;
|
|
|
|
private Color color;
|
|
|
|
private char sex;
|
|
|
|
private float weight;
|
2004-08-20 09:44:42 -04:00
|
|
|
private int litterId;
|
|
|
|
|
|
|
|
private Cat mother;
|
|
|
|
private Set kittens = new HashSet();
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
private void setId(Long id) {
|
|
|
|
this.id=id;
|
|
|
|
}
|
|
|
|
public Long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBirthdate(Date date) {
|
|
|
|
birthdate = date;
|
|
|
|
}
|
|
|
|
public Date getBirthdate() {
|
|
|
|
return birthdate;
|
|
|
|
}
|
2004-08-20 09:44:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
void setWeight(float weight) {
|
|
|
|
this.weight = weight;
|
|
|
|
}
|
|
|
|
public float getWeight() {
|
|
|
|
return weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
void setColor(Color color) {
|
|
|
|
this.color = color;
|
|
|
|
}
|
2004-08-20 09:44:42 -04:00
|
|
|
|
|
|
|
void setSex(char sex) {
|
|
|
|
this.sex=sex;
|
|
|
|
}
|
|
|
|
public char getSex() {
|
|
|
|
return sex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLitterId(int id) {
|
|
|
|
this.litterId = id;
|
|
|
|
}
|
|
|
|
public int getLitterId() {
|
|
|
|
return litterId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMother(Cat mother) {
|
|
|
|
this.mother = mother;
|
|
|
|
}
|
|
|
|
public Cat getMother() {
|
|
|
|
return mother;
|
|
|
|
}
|
2004-06-03 12:31:32 -04:00
|
|
|
void setKittens(Set kittens) {
|
|
|
|
this.kittens = kittens;
|
|
|
|
}
|
|
|
|
public Set getKittens() {
|
|
|
|
return kittens;
|
|
|
|
}
|
2004-08-20 09:44:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
// addKitten not needed by Hibernate
|
|
|
|
public void addKitten(Cat kitten) {
|
2004-08-20 09:44:42 -04:00
|
|
|
kitten.setMother(this);
|
|
|
|
kitten.setLitterId( kittens.size() );
|
2004-06-03 12:31:32 -04:00
|
|
|
kittens.add(kitten);
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
There are four main rules to follow here:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
2004-08-17 12:03:26 -04:00
|
|
|
<sect2 id="persistent-classes-pojo-constructor" revision="1">
|
|
|
|
<title>Implement a no-argument constructor</title>
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<para>
|
2005-06-05 03:28:53 -04:00
|
|
|
<literal>Cat</literal> has a no-argument constructor. All persistent classes must
|
|
|
|
have a default constructor (which may be non-public) so that Hibernate can instantiate
|
|
|
|
them using <literal>Constructor.newInstance()</literal>. We strongly recommend having a
|
|
|
|
default constructor with at least <emphasis>package</emphasis> visibility for runtime proxy
|
|
|
|
generation in Hibernate.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
2005-01-30 07:54:29 -05:00
|
|
|
<sect2 id="persistent-classes-pojo-identifier" revision="2">
|
2004-06-03 12:31:32 -04:00
|
|
|
<title>Provide an identifier property (optional)</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<literal>Cat</literal> has a property called <literal>id</literal>. This property
|
2005-01-27 08:34:18 -05:00
|
|
|
maps to the primary key column of a database table. The property might have been called
|
2004-06-03 12:31:32 -04:00
|
|
|
anything, and its type might have been any primitive type, any primitive "wrapper"
|
|
|
|
type, <literal>java.lang.String</literal> or <literal>java.util.Date</literal>. (If
|
|
|
|
your legacy database table has composite keys, you can even use a user-defined class
|
2004-08-17 12:03:26 -04:00
|
|
|
with properties of these types - see the section on composite identifiers later.)
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-01-30 07:54:29 -05:00
|
|
|
The identifier property is strictly optional. You can leave them off and let Hibernate
|
|
|
|
keep track of object identifiers internally. We do not recommend this, however.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-01-30 07:54:29 -05:00
|
|
|
In fact, some functionality is available only to classes which declare an
|
2004-06-03 12:31:32 -04:00
|
|
|
identifier property:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<itemizedlist spacing="compact">
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2005-01-30 07:54:29 -05:00
|
|
|
Transitive reattachment for detached objects (cascade update or cascade
|
2005-02-06 19:51:16 -05:00
|
|
|
merge) - see <xref linkend="objectstate-transitive"/>
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>Session.saveOrUpdate()</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
2005-01-30 07:54:29 -05:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>Session.merge()</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
2004-06-03 12:31:32 -04:00
|
|
|
</itemizedlist>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
We recommend you declare consistently-named identifier properties on persistent
|
|
|
|
classes. We further recommend that you use a nullable (ie. non-primitive) type.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="persistent-classes-pojo-final">
|
|
|
|
<title>Prefer non-final classes (optional)</title>
|
|
|
|
<para>
|
|
|
|
A central feature of Hibernate, <emphasis>proxies</emphasis>, depends upon the
|
|
|
|
persistent class being either non-final, or the implementation of an interface
|
|
|
|
that declares all public methods.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
You can persist <literal>final</literal> classes that do not implement an interface
|
2005-01-27 08:34:18 -05:00
|
|
|
with Hibernate, but you won't be able to use proxies for lazy association fetching -
|
|
|
|
which will limit your options for performance tuning.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
2005-04-03 17:31:28 -04:00
|
|
|
<para>
|
|
|
|
You should also avoid declaring <literal>public final</literal> methods on the
|
2005-04-03 17:38:38 -04:00
|
|
|
non-final classes. If you want to use a class with a <literal>public final</literal>
|
|
|
|
method, you must explicitly disable proying by setting <literal>lazy="false"</literal>.
|
2005-04-03 17:31:28 -04:00
|
|
|
</para>
|
2004-06-03 12:31:32 -04:00
|
|
|
</sect2>
|
|
|
|
|
2005-06-05 03:28:53 -04:00
|
|
|
<sect2 id="persistent-classes-pojo-accessors" revision="2">
|
|
|
|
<title>Declare accessors and mutators for persistent fields (optional)</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<literal>Cat</literal> declares accessor methods for all its persistent fields.
|
|
|
|
Many other ORM tools directly persist instance variables. We believe it is
|
|
|
|
better to provide an indirection between the relational schema and internal
|
|
|
|
data structures of the class. By default, Hibernate persists JavaBeans style
|
|
|
|
properties, and recognizes method names of the form <literal>getFoo</literal>,
|
|
|
|
<literal>isFoo</literal> and <literal>setFoo</literal>. You may switch to direct
|
|
|
|
field access for particular properties, if needed.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Properties need <emphasis>not</emphasis> be declared public - Hibernate can
|
|
|
|
persist a property with a default, <literal>protected</literal> or
|
|
|
|
<literal>private</literal> get / set pair.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="persistent-classes-inheritance">
|
|
|
|
<title>Implementing inheritance</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A subclass must also observe the first and second rules. It inherits its
|
2004-08-20 09:44:42 -04:00
|
|
|
identifier property from the superclass, <literal>Cat</literal>.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[package eg;
|
|
|
|
|
|
|
|
public class DomesticCat extends Cat {
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
protected void setName(String name) {
|
|
|
|
this.name=name;
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
</sect1>
|
|
|
|
|
2005-01-27 08:34:18 -05:00
|
|
|
<sect1 id="persistent-classes-equalshashcode" revision="1">
|
2004-06-03 12:31:32 -04:00
|
|
|
<title>Implementing <literal>equals()</literal> and <literal>hashCode()</literal></title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
You have to override the <literal>equals()</literal> and <literal>hashCode()</literal>
|
2004-08-20 09:44:42 -04:00
|
|
|
methods if you
|
2005-01-27 08:34:18 -05:00
|
|
|
</para>
|
2004-08-20 09:44:42 -04:00
|
|
|
<itemizedlist spacing="compact">
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
intend to put instances of persistent classes in a <literal>Set</literal>
|
|
|
|
(the recommended way to represent many-valued associations)
|
2005-01-27 08:34:18 -05:00
|
|
|
<emphasis>and</emphasis>
|
|
|
|
</para>
|
2004-08-20 09:44:42 -04:00
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
intend to use reattachment of detached instances
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
<para>
|
2004-08-20 09:44:42 -04:00
|
|
|
Hibernate guarantees equivalence of persistent identity (database row) and Java identity
|
2005-01-27 08:34:18 -05:00
|
|
|
only inside a particular session scope. So as soon as we mix instances retrieved in
|
|
|
|
different sessions, we must implement <literal>equals()</literal> and
|
|
|
|
<literal>hashCode()</literal> if we wish to have meaningful semantics for
|
|
|
|
<literal>Set</literal>s.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The most obvious way is to implement <literal>equals()</literal>/<literal>hashCode()</literal>
|
|
|
|
by comparing the identifier value of both objects. If the value is the same, both must
|
|
|
|
be the same database row, they are therefore equal (if both are added to a <literal>Set</literal>,
|
|
|
|
we will only have one element in the <literal>Set</literal>). Unfortunately, we can't use that
|
2004-08-20 09:44:42 -04:00
|
|
|
approach with generated identifiers! Hibernate will only assign identifier values to objects
|
2005-01-27 08:34:18 -05:00
|
|
|
that are persistent, a newly created instance will not have any identifier value! Furthermore,
|
|
|
|
if an instance is unsaved and currently in a <literal>Set</literal>, saving it will assign
|
|
|
|
an identifier value to the object. If <literal>equals()</literal> and <literal>hashCode()</literal>
|
|
|
|
are based on the identifier value, the hash code would change, breaking the contract of the
|
|
|
|
<literal>Set</literal>. See the Hibernate website for a full discussion of this problem. Note
|
|
|
|
that this is not a Hibernate issue, but normal Java semantics of object identity and equality.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-01-27 08:34:18 -05:00
|
|
|
We recommend implementing <literal>equals()</literal> and <literal>hashCode()</literal>
|
|
|
|
using <emphasis>Business key equality</emphasis>. Business key equality means that the
|
|
|
|
<literal>equals()</literal> method compares only the properties that form the business
|
|
|
|
key, a key that would identify our instance in the real world (a
|
|
|
|
<emphasis>natural</emphasis> candidate key):
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[public class Cat {
|
|
|
|
|
|
|
|
...
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (this == other) return true;
|
2004-08-20 09:44:42 -04:00
|
|
|
if ( !(other instanceof Cat) ) return false;
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
final Cat cat = (Cat) other;
|
|
|
|
|
2004-08-20 09:44:42 -04:00
|
|
|
if ( !cat.getLitterId().equals( getLitterId() ) ) return false;
|
|
|
|
if ( !cat.getMother().equals( getMother() ) ) return false;
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
int result;
|
2004-08-20 09:44:42 -04:00
|
|
|
result = getMother().hashCode();
|
|
|
|
result = 29 * result + getLitterId();
|
2004-06-03 12:31:32 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}]]></programlisting>
|
|
|
|
|
2005-01-27 08:34:18 -05:00
|
|
|
<para>
|
|
|
|
Note that a business key does not have to be as solid as a database
|
2005-02-28 07:43:52 -05:00
|
|
|
primary key candidate (see <xref linkend="transactions-basics-identity"/>).
|
|
|
|
Immutable or unique properties are usually good
|
2005-01-27 08:34:18 -05:00
|
|
|
candidates for a business key.
|
|
|
|
</para>
|
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</sect1>
|
|
|
|
|
2005-03-16 06:44:48 -05:00
|
|
|
<sect1 id="persistent-classes-dynamicmodels">
|
2005-03-30 08:27:16 -05:00
|
|
|
<title>Dynamic models</title>
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<para>
|
2005-03-16 06:44:48 -05:00
|
|
|
<emphasis>Note that the following features are currently considered
|
|
|
|
experimental and may change in the near future.</emphasis>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Persistent entities don't necessarily have to be represented as POJO classes
|
|
|
|
or as JavaBean objects at runtime. Hibernate also supports dynamic models
|
|
|
|
(using <literal>Map</literal>s of <literal>Map</literal>s at runtime) and the
|
|
|
|
representation of entities as DOM4J trees. With this approach, you don't
|
|
|
|
write persistent classes, only mapping files.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
By default, Hibernate works in normal POJO mode. You may set a default entity
|
|
|
|
representation mode for a particular <literal>SessionFactory</literal> using the
|
|
|
|
<literal>default_entity_mode</literal> configuration option (see
|
|
|
|
<xref linkend="configuration-optional-properties"/>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following examples demonstrates the representation using <literal>Map</literal>s.
|
|
|
|
First, in the mapping file, an <literal>entity-name</literal> has to be declared
|
|
|
|
instead of (or in addition to) a class name:
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
2004-08-17 12:03:26 -04:00
|
|
|
<programlisting><![CDATA[<hibernate-mapping>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2005-03-16 06:44:48 -05:00
|
|
|
<class entity-name="Customer">
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2004-08-20 09:44:42 -04:00
|
|
|
<id name="id"
|
2005-02-19 02:47:15 -05:00
|
|
|
type="long"
|
|
|
|
column="ID">
|
2004-08-17 12:03:26 -04:00
|
|
|
<generator class="sequence"/>
|
|
|
|
</id>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
|
|
|
<property name="name"
|
2005-02-19 02:47:15 -05:00
|
|
|
column="NAME"
|
|
|
|
type="string"/>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
|
|
|
<property name="address"
|
2005-02-19 02:47:15 -05:00
|
|
|
column="ADDRESS"
|
|
|
|
type="string"/>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2005-03-16 06:44:48 -05:00
|
|
|
<many-to-one name="organization"
|
|
|
|
column="ORGANIZATION_ID"
|
|
|
|
class="Organization"/>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2005-03-16 06:44:48 -05:00
|
|
|
<bag name="orders"
|
2005-02-19 02:47:15 -05:00
|
|
|
inverse="true"
|
|
|
|
lazy="false"
|
|
|
|
cascade="all">
|
2005-03-16 06:44:48 -05:00
|
|
|
<key column="CUSTOMER_ID"/>
|
|
|
|
<one-to-many class="Order"/>
|
2004-08-17 12:03:26 -04:00
|
|
|
</bag>
|
2005-01-27 08:34:18 -05:00
|
|
|
|
2005-02-19 02:47:15 -05:00
|
|
|
</class>
|
2004-08-20 09:44:42 -04:00
|
|
|
|
2004-08-17 12:03:26 -04:00
|
|
|
</hibernate-mapping>]]></programlisting>
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<para>
|
2005-03-16 06:44:48 -05:00
|
|
|
|
|
|
|
Note that even though associations are declared using target class names,
|
|
|
|
the target type of an associations may also be a dynamic entity instead
|
|
|
|
of a POJO.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
After setting the default entity mode to <literal>dynamic-map</literal>
|
|
|
|
for the <literal>SessionFactory</literal>, we can at runtime work with
|
|
|
|
<literal>Map</literal>s of <literal>Map</literal>s:
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
2004-08-17 12:03:26 -04:00
|
|
|
<programlisting><![CDATA[Session s = openSession();
|
2005-03-16 06:44:48 -05:00
|
|
|
Transaction tx = s.beginTransaction();
|
|
|
|
Session s = openSession();
|
|
|
|
|
|
|
|
// Create a customer
|
|
|
|
Map david = new HashMap();
|
|
|
|
david.put("name", "David");
|
|
|
|
|
|
|
|
// Create an organization
|
|
|
|
Map foobar = new HashMap();
|
|
|
|
foobar.put("name", "Foobar Inc.");
|
|
|
|
|
|
|
|
// Link both
|
|
|
|
david.put("organization", foobar);
|
|
|
|
|
|
|
|
// Save both
|
|
|
|
s.save("Customer", david);
|
|
|
|
s.save("Organization", foobar);
|
|
|
|
|
|
|
|
tx.commit();
|
2004-08-20 09:44:42 -04:00
|
|
|
s.close();]]></programlisting>
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<para>
|
2005-01-23 19:18:43 -05:00
|
|
|
The advantages of a dynamic mapping are quick turnaround time for prototyping
|
|
|
|
without the need for entity class implementation. However, you lose compile-time
|
|
|
|
type checking and will very likely deal with many exceptions at runtime. Thanks
|
|
|
|
to the Hibernate mapping, the database schema can easily be normalized and sound,
|
|
|
|
allowing to add a proper domain model implementation on top later on.
|
2004-06-03 12:31:32 -04:00
|
|
|
</para>
|
|
|
|
|
2005-03-16 06:44:48 -05:00
|
|
|
<para>
|
|
|
|
Entity representation modes can also be set on a per <literal>Session</literal>
|
|
|
|
basis:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[Session dynamicSession = pojoSession.getSession(EntityMode.MAP);
|
|
|
|
|
|
|
|
// Create a customer
|
|
|
|
Map david = new HashMap();
|
|
|
|
david.put("name", "David");
|
|
|
|
dynamicSession.save("Customer", david);
|
|
|
|
...
|
|
|
|
dynamicSession.flush();
|
|
|
|
dynamicSession.close()
|
|
|
|
...
|
|
|
|
// Continue on pojoSession
|
|
|
|
]]></programlisting>
|
|
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Please note that the call to <literal>getSession()</literal> using an
|
|
|
|
<literal>EntityMode</literal> is on the <literal>Session</literal> API, not the
|
|
|
|
<literal>SessionFactory</literal>. That way, the new <literal>Session</literal>
|
|
|
|
shares the underlying JDBC connection, transaction, and other context
|
|
|
|
information. This means you don't have tocall <literal>flush()</literal>
|
|
|
|
and <literal>close()</literal> on the secondary <literal>Session</literal>, and
|
|
|
|
also leave the transaction and connection handling to the primary unit of work.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
More information about the XML representation capabilities can be found
|
|
|
|
in <xref linkend="xml"/>.
|
|
|
|
</para>
|
|
|
|
|
2005-07-14 22:47:54 -04:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="persistent-classes-tuplizers" revision="0">
|
|
|
|
<title>Tuplizers</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<literal>org.hibernate.tuple.Tuplizer</literal>, and its sub-interfaces, are responsible
|
|
|
|
for managing a particular representation of a piece of data, given that representation's
|
|
|
|
<literal>org.hibernate.EntityMode</literal>. If a given piece of data is thought of as
|
|
|
|
a data structure, then a tuplizer is the thing which knows how to create such a data structure
|
|
|
|
and how to extract values from and inject values into such a data structure. For example,
|
|
|
|
for the POJO entity mode, the correpsonding tuplizer knows how create the POJO through its
|
|
|
|
constructor and how to access the POJO properties using the defined property accessors.
|
|
|
|
There are two high-level types of Tuplizers, represented by the
|
|
|
|
<literal>org.hibernate.tuple.EntityTuplizer</literal> and <literal>org.hibernate.tuple.ComponentTuplizer</literal>
|
|
|
|
interfaces. <literal>EntityTuplizer</literal>s are responsible for managing the above mentioned
|
|
|
|
contracts in regards to entities, while <literal>ComponentTuplizer</literal>s do the same for
|
|
|
|
components.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Users may also plug in their own tuplizers. Perhaps you require that a <literal>java.util.Map</literal>
|
|
|
|
implementation other than <literal>java.util.HashMap</literal> be used while in the
|
|
|
|
dynamic-map entity-mode; or perhaps you need to define a different proxy generation strategy
|
|
|
|
than the one used by default. Both would be achieved by defining a custom tuplizer
|
|
|
|
implementation. Tuplizers definitions are attached to the entity or component mapping they
|
|
|
|
are meant to manage. Going back to the example of our customer entity:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[<hibernate-mapping>
|
|
|
|
<class entity-name="Customer">
|
|
|
|
<!--
|
|
|
|
Override the dynamic-map entity-mode
|
|
|
|
tuplizer for the customer entity
|
|
|
|
-->
|
|
|
|
<tuplizer entity-mode="dynamic-map"
|
|
|
|
class="CustomMapTuplizerImpl"/>
|
|
|
|
|
|
|
|
<id name="id" type="long" column="ID">
|
|
|
|
<generator class="sequence"/>
|
|
|
|
</id>
|
|
|
|
|
|
|
|
<!-- other properties -->
|
|
|
|
...
|
|
|
|
</class>
|
|
|
|
</hibernate-mapping>
|
|
|
|
|
|
|
|
|
|
|
|
public class CustomMapTuplizerImpl
|
|
|
|
extends org.hibernate.tuple.DynamicMapEntityTuplizer {
|
|
|
|
// override the buildInstantiator() method to plug in our custom map...
|
|
|
|
protected final Instantiator buildInstantiator(
|
|
|
|
org.hibernate.mapping.PersistentClass mappingInfo) {
|
|
|
|
return new CustomMapInstantiator( mappingInfo );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class CustomMapInstantiator
|
|
|
|
extends org.hibernate.tuple.DynamicMapInstantitor {
|
|
|
|
// override the generateMap() method to return our custom map...
|
|
|
|
protected final Map generateMap() {
|
|
|
|
return new CustomMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
|
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</sect1>
|
|
|
|
|
2005-01-23 19:18:43 -05:00
|
|
|
<para>
|
2005-02-19 02:47:15 -05:00
|
|
|
TODO: Document user-extension framework in the property and proxy packages
|
2005-01-23 19:18:43 -05:00
|
|
|
</para>
|
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</chapter>
|
|
|
|
|