Глава 5. Основы объектно-реляционного маппинга

5.1. Объявление маппинга

Объектно-реляционный маппинг описывается в виде XML документа. Документ описывающий маппинг проектировался как легко читаемый и ориентированный на ручное редактирования. Язык описания маппинга ориентирован на Java, это означает что маппинг констуируются вокруг объявлений персистентных java-классов, а не таблиц БД.

Несмотря на то, что большинство пользователей Hibernate определяют и редактируют маппинги в XML вручную, существует некоторое количество инструментов для генерации документов описывающих маппинги. В качестве примеров можно привести XDoclet, Middlegen и AndroMDA.

Начнем с примера описания маппинга:

<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="eg">

        <class name="Cat" table="CATS" discriminator-value="C">
                <id name="id" column="uid" type="long">
                        <generator class="hilo"/>
                </id>
                <discriminator column="subclass" type="character"/>
                <property name="birthdate" type="date"/>
                <property name="color" not-null="true"/>
                <property name="sex" not-null="true" update="false"/>
                <property name="weight"/>
                <many-to-one name="mate" column="mate_id"/>
                <set name="kittens">
                        <key column="mother_id"/>
                        <one-to-many class="Cat"/>
                </set>
                <subclass name="DomesticCat" discriminator-value="D">
                        <property name="name" type="string"/>
                </subclass>
        </class>

        <class name="Dog">
                <!-- Здесь описывается маппинг для собаки -->
        </class>

</hibernate-mapping>

Мы не будем рассматривать содержание документа маппинга. Мы опишем только те элементы и атрибуты документа, которые используются Hibernate в процессе работы. Приведенный выше документ маппинга среди прочего содержит атрибуты и элементы которые используются при генерации схемы базы данных, например атрибут not-null.

5.1.1. Тип документа (Doctype)

Все XML документы описывающие маппинги должны содержать определение типа документа (doctype). Актуальный DTD может быть найден по URL, определенному выше, в директории hibernate-x.x.x/src/net/sf/hibernate или в файле hibernate.jar. Hibernate всегда сначала ищет DTD в classpath.

5.1.2. hibernate-mapping

Элемент имеет три необязательных атрибута. Атрибут schema определяет схему, в терминах СУБД, к которой относятся таблицы описываемые в документе маппинга. Если атрибут указан, Hibernate всегда именует таблицы с указанием схемы, в противном случае, имена таблиц формируются без указания схемы. Атрибут default-cascade определяет правила каскадов для свойств и коллекций, в описании которых явно не определен атрибут cascade. Атрибут auto-import позволяет использовать неполные имена классов (без имени пакета) в языке запросов.

<hibernate-mapping
         schema="schemaName"                          (1)
         default-cascade="none|save-update"           (2)
         auto-import="true|false"                     (3)
         package="package.name"                       (4)
 />
(1)

schema (необязательный): Наименование схемы БД.

(2)

default-cascade (необязательный - по умолчанию none): Правила каскадов по умолчанию.

(3)

auto-import (необязательный - по умолчанию true): Определяет возможность использования неполных имен классов (описанных в данном документе) в языке запросов.

(4)

package (необязательный): Префикс пакета для определения полного наименования классов.

Необходимо устанавливать auto-import="false", если существует два или более персистентных класса с одинаковыми наименованиями (без учета имени пакета). Если Hibernate обнаружит два класса в разных пакетах соответсвующих указаному имени, при разборе файла маппинга будет сгенерировано исключение.

5.1.3. class

Персистентные классы определяются в элементе class:

<class
        name="ClassName"                              (1)
        table="tableName"                             (2)
        discriminator-value="discriminator_value"     (3)
        mutable="true|false"                          (4)
        schema="owner"                                (5)
        proxy="ProxyInterface"                        (6)
        dynamic-update="true|false"                   (7)
        dynamic-insert="true|false"                   (8)
        select-before-update="true|false"             (9)
        polymorphism="implicit|explicit"              (10)
        where="arbitrary sql where condition"         (11)
        persister="PersisterClass"                    (12)
        batch-size="N"                                (13)
        optimistic-lock="none|version|dirty|all"      (14)
        lazy="true|false"                             (15)
/>
(1)

name: Полное наименование Java класса персистентного класса или интерфейса.

(2)

table: Наименование таблицы БД.

(3)

discriminator-value (необязательный - по умолчанию имя класса): Значение для определения индивидуальных подклассов. Используется при полиморфизме. Допустимые значения включают null и not null

(4)

mutable (необязательный, по умолчанию true): Используется для определения (не)изменчивости класса (mutable/not mutable)

(5)

schema (необязательный): Переопределяет наименование схемы которая была специфицирована в корневом элементе <hibernate-mapping>.

(6)

proxy (необязательный): Определяет интерфейс используемый для ленивой инициализации прокси-класса. В качестве прокси-класса можно использовать тот же (персистетный) класс.

(7)

dynamic-update (необязательно, по умолчанию false): Если параметр установлен в true Hibernate будет генерировать UPDATE запрос в процессе выполнения, при этом запрос будет сгенерирован только на те столбцы, которые были изменены.

(8)

dynamic-insert (необязательный, по умолчанию false): Если параметр установлен в true Hibernate будет генерировать INSERT запрос в процессе выполнения, при этом запрос будет сгенерирован только на те столбцы, значение которых не null.

(9)

select-before-update (необязательный, по умолчанию false): Обязывает Hibernate перед выполнением SQL UPDATE всегда проверять действительно ли изменился объект. В определенных случаях (в действительности, только когда трансиентный объект ассоциируется с новой сессией с использованием update()), это означает что Hibernate генерирует SQL SELECT чтобы убедиться в том, что UPDATE действительно необходим.

(10)

polymorphism (необязательный, по умолчанию implicit): Определяет использования явного (explicit) или неявного (implicit) полиморфизма.

(11)

where (необязательный) определяет произвольное условие WHERE SQL запроса, который используется для получения обьектов определяемого класса.

(12)

persister (необязательный): Используется для указания реализации ClassPersister.

(13)

batch-size (необязательный, по умолчанию 1) определяет "размер пакета (batch size)" для выборки экземпляров определяемого класса по идентификатору.

(14)

optimistic-lock (необязательный, по умолчанию version): Устанавливает стратегию оптимистического блокирования (optimistic locking)

(15)

lazy (необязательный): Установка атрибута lazy="true" является эквивалентом установки интерфеса прокси на себя (смотри атрибут proxy).

Совершенно корректно использование java интерфейса в качестве персистентного класса. Реализации инфтрфейса можно определить при помощи элемента <subclass>. Так же возможно использование статических внутренних классов (static inner class), при этом следует использовать стандартную форму наименоваия класса (например eg.Foo$Bar)

Неизменяемые классы (immutable classes), с установленым атрибутом mutable="false" не могут быть изменены или удалены приложением. Это позволяет Hibernate немного оптимизировать скорость работы с такими классами.

Необязательный атрибут proxy разрешает ленивую инициализацию экземпляров персистентного класса. Изначально Hibernate возвращает CGLIB прокси, реализующие указанный интерфейс, и только после первого вызова метода прокси интерфейса загружается сам персистентный класс. Смотри так же раздел "Прокси для ленивой инициализации" ниже.

Неявный (implicit) полиморфизм означает что экземпляры класса будут созданы для запроса по любому суперклассу или реализованному персистентным классом интерфейсу. Явный (explicit) полиморфизм означает, что экземпляры класса будут создаваться только при запросах с явным указанием имени персистентного класса. TODO: Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only be queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>. For most purposes the default, polymorphism="implicit", is appropriate. Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns).

Атрибут persister позволяет настроить стратегию персистенции для класса. Например, вы можете написать свою реализацю net.sf.hibernate.persister.EntityPersister или же совершенно иную реализацию интерфейса net.sf.hibernate.persister.ClassPersister которая будет реализовывать персистенцию, через, например, вызов хранимых процедур, сериализацию в файловую струкуру или LDAP. Смотри для примера net.sf.hibernate.test.CustomPersister (реализует "персистентность" в Hashtable).

Учтите что установки dynamic-update и dynamic-insert не наследуются подклассами должны быть повторно установлены в элеметах <subclass> или <joined-subclass>. Эти установки могут увеличить производительность в некоторых случаях, но так же возможно и уменьшение производительности в иных случаях. Используйте их рассудительно.

Использование select-before-update обычно уменьшает производительность. Используется, чтобы избежать лишних вызовов тригеров на update.

Если вы включили dynamic-update, у вас есть выбор стратегии оптимистической блокировки (optimistic locking):

  • version проверять столбец версии/даты

  • all проверять все столбцы

  • dirty проверять измененные столбцы

  • none не использовать оптимистичесую блокировку

Мы очень настойчиво рекомендуем использовать стобцы версии/даты для оптимистической блокировки в Hibernate. Эта стратегия обеспечивает оптимальную производительность и это единственная стратегия обеспечивающая корректное отслеживание изменений, которые производились все контекста сессии (при использовании Session.update()). Помните, что свойство версии или даты никогда не должны быть null, в противном случае, независимо от стратегии unsaved-value, экземляры будут опознаны как трансиентные (transient).

5.1.4. id

Замеппленые классы должны декларировать столбец первичного ключа в таблице базы данных. Большинство классов так же определяют JavaBean свойсвтво хранящее уникальный идентификатор экземпляра. Элемент <id> определяет маппинг этого свойства к первичному ключу таблицы.

<id
        name="propertyName"                      (1)
        type="typename"                          (2)
        column="column_name"                     (3)
        unsaved-value="any|none|null|id_value"   (4)
        access="field|property|ClassName">       (5)

        <generator class="generatorClass"/>
</id>
(1)

name (необязательный): Наименование свойства идентификатора.

(2)

type (необязательный): Имя определяющее Hibernate-тип свойства.

(3)

column (необязательно - по умолчанию имя свойства): Имя столбца - первичного ключа таблицы.

(4)

unsaved-value (необязательно - по умолчанию null): Значени свойства идентификатора, которое обзначает, что экземпляр новый (в терминах персистентного хранилища). Отличает экземпляр от других трансиентных экземпляров, которые были загружены или сохранены в предыдущей версии.

(5)

access (необязательный - по умолчанию property): Стратегия, которую должен использовать Hibernate для доступа к значению свойства.

Если атрибут name не указан, предполагается, что класс не имеет свойства идентификатора.

Атрибут unsaved-value важен! Если свойство идентификатор вашего класса по умолчанию не null, вы должны специфицировать реально существующее умолчание для данного свойства.

Существует альтернативное объявление <composite-id> для доступа к унаследованным (legacy) данным c композитными ключами. Мы настойчиво не рекомендуем использовать композитные ключи в других случаях.

5.1.4.1. generator

Обязательный дочерний элемент <generator>'а определяет Java класс используемый для генерации уникальных идентификаторов экземпляров песистентных классов. При необходимости используется элемент <param> для передачи параметров инициализации или конфигурирации экземпляра генератора.

<id name="id" type="long" column="uid" unsaved-value="0">
        <generator class="net.sf.hibernate.id.TableHiLoGenerator">
                <param name="table">uid_table</param>
                <param name="column">next_hi_value_column</param>
        </generator>
</id>

Все генераторы реализуют интерфейс net.sf.hibernate.id.IdentifierGenerator. Это очень простой интерфейс; многие приложения могут использовать свою специальную реализацию генератора. Несмотря на это, Hibernate включает в себя множество встроенных генераторов. Ниже идут краткие наименования (ярлыки) для встроенных генераторов:

increment

генерирует идентификаторы типа long, short или int, уникальные только когда другие процессы не добавляют данные в ту же таблицу. Не использовать в кластере.

identity

Поддерживает identity столбцы в in DB2, MySQL, MS SQL Server, Sybase и HypersonicSQL. Тип возвращаемого идентификатора long, short или int.

sequence

Использует sequence в DB2, PostgreSQL, Oracle, SAP DB, McKoi или generator в Interbase. Тип возвращаемого идентификатора long, short или int.

hilo

использует hi/lo алгоритм для эффективной генерации идентификаторов которые имеют тип long, short или int, требуют наименования таблицы и столбца (по умолчанию hibernate_unique_key и next_hi соответсвенно), как источник значений hi. Алгоритм hi/lo генерирует идентификаторы которые кникальный только для отдельный баз данных. Не используйте этот генератор для соединений через JTA или пользовательских соединений.

seqhilo

использует алгоритм hi/lo для эффективной генерации идентификаторов типа long, short или int, с использованием последовательности (sequence) базы данных.

uuid.hex

Использует 128-битный UUID алгоритм для генерации строковых идентификаторов, уникальных в сети (изспользуется IP-адрес). UUID - строка длинной в 32 символа, содержащая шеснадцатеричное представление числа.

uuid.string

использует тот же UUID алгоритм, однако строка при использовании этого генератора состоит из 16 (каких-то) ANSII символов. Не использовать с PostgreSQL.

native

выбирает identity, sequence или hilo, в зависимости от возможностей используемой базы данных.

assigned

оставляет отвественность по назначению идентификатора приложению. Предполагается, что приложение устанавливает идентификатор перед вызовом save().

foreign

используется идентификатор другого, ассоциированного объекта. Обычно используется в крньюкции с <one-to-one> оассоциацией по первичному ключу.

5.1.4.2. Алгоритм Hi/Lo

Генераторы hilo и seqhilo предоставляют две альтернативные реализации алгортма hi/lo, наиболее предпочтительного подхода для генерации идентификаторов. Первая реализация требует "специальной" таблицы в базе данных, для хранения следующего "hi" значения. Вторая реализация использует последовательность (Oracle-style), в базах данных, которые их поддерживают.

<id name="id" type="long" column="cat_id">
        <generator class="hilo">
                <param name="table">hi_value</param>
                <param name="column">next_value</param>
                <param name="max_lo">100</param>
        </generator>
</id>
<id name="id" type="long" column="cat_id">
        <generator class="seqhilo">
                <param name="sequence">hi_value</param>
                <param name="max_lo">100</param>
        </generator>
</id>

К сожалению вы не можете использовать hilo в случае поставки своего соединения (Connection) в Hibernate, так же невозможно его использование в конфигурации, когда Hiberante получает соединение к базе данных используя datasource через JTA. Hiberante должен иметь возможность получать "hi" значение в новой тразакции. Стандартным подходом в EJB, является использование session stateless bean для реализации алгоритма hi/lo.

5.1.4.3. Алгоритм UUID

The UUIDs contain: IP address, startup time of the JVM (accurate to a quarter second), system time and a counter value (unique within the JVM). It's not possible to obtain a MAC address or memory address from Java code, so this is the best we can do without using JNI.

Don't try to use uuid.string in PostgreSQL.

5.1.4.4. Identity columns and Sequences

For databases which support identity columns (DB2, MySQL, Sybase, MS SQL), you may use identity key generation. For databases that support sequences (DB2, Oracle, PostgreSQL, Interbase, McKoi, SAP DB) you may use sequence style key generation. Both these strategies require two SQL queries to insert a new object.

<id name="id" type="long" column="uid">
        <generator class="sequence">
                <param name="sequence">uid_sequence</param>
        </generator>
</id>
<id name="id" type="long" column="uid" unsaved-value="0">
        <generator class="identity"/>
</id>

For cross-platform development, the native strategy will choose from the identity, sequence and hilo strategies, dependant upon the capabilities of the underlying database.

5.1.4.5. Assigned Identifiers

If you want the application to assign identifiers (as opposed to having Hibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

Due to its inherent nature, entities that use this generator cannot be saved via the Session's saveOrUpdate() method. Instead you have to explicitly specify to Hibernate if the object should be saved or updated by calling either the save() or update() method of the Session.

5.1.5. composite-id

<composite-id
        name="propertyName"
        class="ClassName"
        unsaved-value="any|none"
        access="field|property|ClassName">

        <key-property name="propertyName" type="typename" column="column_name"/>
        <key-many-to-one name="propertyName class="ClassName" column="column_name"/>
        ......
</composite-id>

For a table with a composite key, you may map multiple properties of the class as identifier properties. The <composite-id> element accepts <key-property> property mappings and <key-many-to-one> mappings as child elements.

<composite-id>
        <key-property name="medicareNumber"/>
        <key-property name="dependent"/>
</composite-id>

Your persistent class must override equals() and hashCode() to implement composite identifier equality. It must also implements Serializable.

Unfortunately, this approach to composite identifiers means that a persistent object is its own identifier. There is no convenient "handle" other than the object itself. You must instantiate an instance of the persistent class itself and populate its identifier properties before you can load() the persistent state associated with a composite key. We will describe a much more convenient approach where the composite identifier is implemented as a seperate class in Раздел 7.4, «Компоненты как составные идентификаторы». The attributes described below apply only to this alternative approach:

  • name (optional): A property of component type that holds the composite identifier (see next section).

  • class (optional - defaults to the property type determined by reflection): The component class used as a composite identifier (see next section).

  • unsaved-value (optional - defaults to none): Indicates that transient instances should be considered newly instantiated, if set to any.

5.1.6. discriminator

The <discriminator> element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row. A restricted set of types may be used: string, character, integer, byte, short, boolean, yes_no, true_false.

<discriminator
        column="discriminator_column"  (1)
        type="discriminator_type"      (2)
        force="true|false"             (3)
/>
(1)

column (optional - defaults to class) the name of the discriminator column.

(2)

type (optional - defaults to string) a name that indicates the Hibernate type

(3)

force (optional - defaults to false) "force" Hibernate to specify allowed discriminator values even when retrieving all instances of the root class.

Actual values of the discriminator column are specified by the discriminator-value attribute of the <class> and <subclass> elements.

The force attribute is (only) useful if the table contains rows with "extra" discriminator values that are not mapped to a persistent class. This will not usually be the case.

5.1.7. version (optional)

The <version> element is optional and indicates that the table contains versioned data. This is particularly useful if you plan to use long transactions (see below).

<version
        column="version_column"                            (1)
        name="propertyName"                                (2)
        type="typename"                                    (3)
        access="field|property|ClassName"                  (4)
        unsaved-value="null|negative|undefined"            (5)
/>
(1)

column (optional - defaults to the property name): The name of the column holding the version number.

(2)

name: The name of a property of the persistent class.

(3)

type (optional - defaults to integer): The type of the version number.

(4)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

(5)

unsaved-value (optional - defaults to undefined): A version property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session. (undefined specifies that the identifier property value should be used.)

Version numbers may be of type long, integer, short, timestamp or calendar.

5.1.8. timestamp (optional)

The optional <timestamp> element indicates that the table contains timestamped data. This is intended as an alternative to versioning. Timestamps are by nature a less safe implementation of optimistic locking. However, sometimes the application might use the timestamps in other ways.

<timestamp
        column="timestamp_column"           (1)
        name="propertyName"                 (2)
        access="field|property|ClassName"   (3)
        unsaved-value="null|undefined"      (4)
/>
(1)

column (optional - defaults to the property name): The name of a column holding the timestamp.

(2)

name: The name of a JavaBeans style property of Java type Date or Timestamp of the persistent class.

(3)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

(4)

unsaved-value (optional - defaults to null): A version property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session. (undefined specifies that the identifier property value should be used.)

Note that <timestamp> is equivalent to <version type="timestamp">.

5.1.9. property

The <property> element declares a persistent, JavaBean style property of the class.

<property
        name="propertyName"                 (1)
        column="column_name"                (2)
        type="typename"                     (3)
        update="true|false"                 (4)
        insert="true|false"                 (4)
        formula="arbitrary SQL expression"  (5)
        access="field|property|ClassName"   (6)
/>
(1)

name: the name of the property, with an initial lowercase letter.

(2)

column (optional - defaults to the property name): the name of the mapped database table column.

(3)

type (optional): a name that indicates the Hibernate type.

(4)

update, insert (optional - defaults to true) : specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.

(5)

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.

(6)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

typename could be:

  1. The name of a Hibernate basic type (eg. integer, string, character, date, timestamp, float, binary, serializable, object, blob).

  2. The name of a Java class with a default basic type (eg. int, float, char, java.lang.String, java.util.Date, java.lang.Integer, java.sql.Clob).

  3. The name of a subclass of PersistentEnum (eg. eg.Color).

  4. The name of a serializable Java class.

  5. The class name of a custom type (eg. com.illflow.type.MyCustomType).

If you do not specify a type, Hibernate will use reflection upon the named property to take a guess at the correct Hibernate type. Hibernate will try to interpret the name of the return class of the property getter using rules 2, 3, 4 in that order. However, this is not always enough. In certain cases you will still need the type attribute. (For example, to distinguish between Hibernate.DATE and Hibernate.TIMESTAMP, or to specify a custom type.)

The access attribute lets you control how Hibernate will access the property at runtime. By default, Hibernate will call the property get/set pair. If you specify access="field", Hibernate will bypass the get/set pair and access the field directly, using reflection. You may specify your own strategy for property access by naming a class that implements the interface net.sf.hibernate.property.PropertyAccessor.

5.1.10. many-to-one

An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association. (Its really just an object reference.)

<many-to-one
        name="propertyName"                                (1)
        column="column_name"                               (2)
        class="ClassName"                                  (3)
        cascade="all|none|save-update|delete"              (4)
        outer-join="true|false|auto"                       (5)
        update="true|false"                                (6)
        insert="true|false"                                (6)
        property-ref="propertyNameFromAssociatedClass"     (7)
        access="field|property|ClassName"                  (8)
/>
(1)

name: The name of the property.

(2)

column (optional): The name of the column.

(3)

class (optional - defaults to the property type determined by reflection): The name of the associated class.

(4)

cascade (optional): Specifies which operations should be cascaded from the parent object to the associated object.

(5)

outer-join (optional - defaults to auto): enables outer-join fetching for this association when hibernate.use_outer_join is set.

(6)

update, insert (optional - defaults to true) specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.

(7)

property-ref: (optional) The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

(8)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

The cascade attribute permits the following values: all, save-update, delete, none. Setting a value other than none will propagate certain operations to the associated (child) object. See "Lifecycle Objects" below.

The outer-join attribute accepts three different values:

  • auto (default) Fetch the association using an outerjoin if the associated class has no proxy

  • true Always fetch the association using an outerjoin

  • false Never fetch the association using an outerjoin

A typical many-to-one declaration looks as simple as

<many-to-one name="product" class="Product" column="PRODUCT_ID"/>

The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is an ugly relational model. For example, suppose the Product class had a unique serial number, that is not the primary key. (The unique attribute controls Hibernate's DDL generation with the SchemaExport tool.)

<property name="serialNumber" unique="true" type="string" column="SERIAL_NUMBER"/>

Then the mapping for OrderItem might use:

<many-to-one name="product" property-ref="serialNumber" column="PRODUCT_SERIAL_NUMBER"/>

This is certainly not encouraged, however.

5.1.11. one-to-one

A one-to-one association to another persistent class is declared using a one-to-one element.

<one-to-one
        name="propertyName"                                (1)
        class="ClassName"                                  (2)
        cascade="all|none|save-update|delete"              (3)
        constrained="true|false"                           (4)
        outer-join="true|false|auto"                       (5)
        property-ref="propertyNameFromAssociatedClass"     (6)
        access="field|property|ClassName"                  (7)
/>
(1)

name: The name of the property.

(2)

class (optional - defaults to the property type determined by reflection): The name of the associated class.

(3)

cascade (optional) specifies which operations should be cascaded from the parent object to the associated object.

(4)

constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which save() and delete() are cascaded (and is also used by the schema export tool).

(5)

outer-join (optional - defaults to auto): Enable outer-join fetching for this association when hibernate.use_outer_join is set.

(6)

property-ref: (optional) The name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used.

(7)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

There are two varieties of one-to-one association:

  • primary key associations

  • unique foreign key associations

Primary key associations don't need an extra table column; if two rows are related by the association then the two table rows share the same primary key value. So if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value!

For a primary key association, add the following mappings to Employee and Person, respectively.

<one-to-one name="person" class="Person"/>
<one-to-one name="employee" class="Employee" constrained="true"/>

Now we must ensure that the primary keys of related rows in the PERSON and EMPLOYEE tables are equal. We use a special Hibernate identifier generation strategy called foreign:

<class name="person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="foreign">
            <param name="property">employee</param>
        </generator>
    </id>
    ...
    <one-to-one name="employee"
        class="Employee"
        constrained="true"/>
</class>

A newly saved instance of Person is then assigned the same primar key value as the Employee instance refered with the employee property of that Person.

Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>

And this association may be made bidirectional by adding the following to the Person mapping:

<one-to-one name"employee" class="Employee" property-ref="person"/>

5.1.12. component, dynamic-component

The <component> element maps properties of a child object to columns of the table of a parent class. Components may, in turn, declare their own properties, components or collections. See "Components" below.

<component 
        name="propertyName"                 (1)
        class="className"                   (2)
        insert="true|false"                 (3)
        upate="true|false"                  (4)
        access="field|property|ClassName">  (5)
        
        <property ...../>
        <many-to-one .... />
        ........
</component>
(1)

name: The name of the property.

(2)

class (optional - defaults to the property type determined by reflection): The name of the component (child) class.

(3)

insert: Do the mapped columns appear in SQL INSERTs?

(4)

update: Do the mapped columns appear in SQL UPDATEs?

(5)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

The child <property> tags map properties of the child class to table columns.

The <component> element allows a <parent> subelement that maps a property of the component class as a reference back to the containing entity.

The <dynamic-component> element allows a Map to be mapped as a component, where the property names refer to keys of the map.

5.1.13. subclass

Finally, polymorphic persistence requires the declaration of each subclass of the root persistent class. For the (recommended) table-per-class-hierarchy mapping strategy, the <subclass> declaration is used.

<subclass
        name="ClassName"                              (1)
        discriminator-value="discriminator_value"     (2)
        proxy="ProxyInterface"                        (3)
        lazy="true|false"                             (4)
        dynamic-update="true|false"
        dynamic-insert="true|false">

        <property .... />
        .....
</subclass>
(1)

name: The fully qualified class name of the subclass.

(2)

discriminator-value (optional - defaults to the class name): A value that distiguishes individual subclasses.

(3)

proxy (optional): Specifies a class or interface to use for lazy initializing proxies.

(4)

lazy (optional): Setting lazy="true" is a shortcut equalivalent to specifying the name of the class itself as the proxy interface.

Each subclass should declare its own persistent properties and subclasses. <version> and <id> properties are assumed to be inherited from the root class. Each subclass in a heirarchy must define a unique discriminator-value. If none is specified, the fully qualified Java class name is used.

5.1.14. joined-subclass

Alternatively, a subclass that is persisted to its own table (table-per-subclass mapping strategy) is declared using a <joined-subclass> element.

<joined-subclass
        name="ClassName"                    (1)
        proxy="ProxyInterface"              (2)
        lazy="true|false"                   (3)
        dynamic-update="true|false"
        dynamic-insert="true|false">

        <key .... >

        <property .... />
        .....
</joined-subclass>
(1)

name: The fully qualified class name of the subclass.

(2)

proxy (optional): Specifies a class or interface to use for lazy initializing proxies.

(3)

lazy (optional): Setting lazy="true" is a shortcut equalivalent to specifying the name of the class itself as the proxy interface.

No discriminator column is required for this mapping strategy. Each subclass must, however, declare a table column holding the object identifier using the <key> element. The mapping at the start of the chapter would be re-written as:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="eg">

        <class name="Cat" table="CATS">
                <id name="id" column="uid" type="long">
                        <generator class="hilo"/>
                </id>
                <property name="birthdate" type="date"/>
                <property name="color" not-null="true"/>
                <property name="sex" not-null="true"/>
                <property name="weight"/>
                <many-to-one name="mate"/>
                <set name="kittens">
                        <key column="MOTHER"/>
                        <one-to-many class="Cat"/>
                </set>
                <joined-subclass name="DomesticCat" table="DOMESTIC_CATS">
                	<key column="CAT"/>
                        <property name="name" type="string"/>
                </joined-subclass>
        </class>

        <class name="eg.Dog">
                <!-- mapping for Dog could go here -->
        </class>

</hibernate-mapping>

5.1.15. map, set, list, bag

Collections are discussed later.

5.1.16. import

Suppose your application has two persistent classes with the same name, and you don't want to specify the fully qualified (package) name in Hibernate queries. Classes may be "imported" explicitly, rather than relying upon auto-import="true". You may even import classes and interfaces that are not explicitly mapped.

<import class="java.lang.Object" rename="Universe"/>
<import
        class="ClassName"              (1)
        rename="ShortName"             (2)
/>
(1)

class: The fully qualified class name of of any Java class.

(2)

rename (optional - defaults to the unqualified class name): A name that may be used in the query language.

5.2. Hibernate Types

5.2.1. Entities and values

To understand the behaviour of various Java language-level objects with respect to the persistence service, we need to classify them into two groups:

An entity exists independently of any other objects holding references to the entity. Contrast this with the usual Java model where an unreferenced object is garbage collected. Entities must be explicitly saved and deleted (except that saves and deletions may be cascaded from a parent entity to its children). This is different from the ODMG model of object persistence by reachablity - and corresponds more closely to how application objects are usually used in large systems. Entities support circular and shared references. They may also be versioned.

An entity's persistent state consists of references to other entities and instances of value types. Values are primitives, collections, components and certain immutable objects. Unlike entities, values (in particular collections and components) are persisted and deleted by reachability. Since value objects (and primitives) are persisted and deleted along with their containing entity they may not be independently versioned. Values have no independent identity, so they cannot be shared by two entities or collections.

All Hibernate types except collections support null semantics.

Up until now, we've been using the term "persistent class" to refer to entities. We will continue to do that. Strictly speaking, however, not all user-defined classes with persistent state are entities. A component is a user defined class with value semantics.

5.2.2. Basic value types

The basic types may be roughly categorized into

integer, long, short, float, double, character, byte, boolean, yes_no, true_false

Type mappings from Java primitives or wrapper classes to appropriate (vendor-specific) SQL column types. boolean, yes_no and true_false are all alternative encodings for a Java boolean or java.lang.Boolean.

string

A type mapping from java.lang.String to VARCHAR (or Oracle VARCHAR2).

date, time, timestamp

Type mappings from java.util.Date and its subclasses to SQL types DATE, TIME and TIMESTAMP (or equivalent).

calendar, calendar_date

Type mappings from java.util.Calendar to SQL types TIMESTAMP and DATE (or equivalent).

big_decimal

A type mapping from java.math.BigDecimal to NUMERIC (or Oracle NUMBER).

locale, timezone, currency

Type mappings from java.util.Locale, java.util.TimeZone and java.util.Currency to VARCHAR (or Oracle VARCHAR2). Instances of Locale and Currency are mapped to their ISO codes. Instances of TimeZone are mapped to their ID.

class

A type mapping from java.lang.Class to VARCHAR (or Oracle VARCHAR2). A Class is mapped to its fully qualified name.

binary

Maps byte arrays to an appropriate SQL binary type.

text

Maps long Java strings to a SQL CLOB or TEXT type.

serializable

Maps serializable Java types to an appropriate SQL binary type. You may also indicate the Hibernate type serializable with the name of a serializable Java class or interface that does not default to a basic type or implement PersistentEnum.

clob, blob

Type mappings for the JDBC classes java.sql.Clob and java.sql.Blob. These types may be inconvenient for some applications, since the blob or clob object may not be reused outside of a transaction. (Furthermore, driver support is patchy and inconsistent.)

Unique identifiers of entities and collections may be of any basic type except binary, blob and clob. (Composite identifiers are also allowed, see below.)

The basic value types have corresponding Type constants defined on net.sf.hibernate.Hibernate. For example, Hibernate.STRING represents the string type.

5.2.3. Persistent enum types

An enumerated type is a common Java idiom where a class has a constant (small) number of immutable instances. You may create a persistent enumerated type by implementing net.sf.hibernate.PersistentEnum, defining the operations toInt() and fromInt():

package eg;
import net.sf.hibernate.PersistentEnum;

public class Color implements PersistentEnum {
    private final int code;
    private Color(int code) {
        this.code = code;
    }
    public static final Color TABBY = new Color(0);
    public static final Color GINGER = new Color(1);
    public static final Color BLACK = new Color(2);

    public int toInt() { return code; }

    public static Color fromInt(int code) {
        switch (code) {
            case 0: return TABBY;
            case 1: return GINGER;
            case 2: return BLACK;
            default: throw new RuntimeException("Unknown color code");
        }
    }
}

The Hibernate type name is simply the name of the enumerated class, in this case eg.Color.

5.2.4. Custom value types

It is relatively easy for developers to create their own value types. For example, you might want to persist properties of type java.lang.BigInteger to VARCHAR columns. Hibernate does not provide a built-in type for this. But custom types are not limited to mapping a property (or collection element) to a single table column. So, for example, you might have a Java property getName()/setName() of type java.lang.String that is persisted to the columns FIRST_NAME, INITIAL, SURNAME.

To implement a custom type, implement either net.sf.hibernate.UserType or net.sf.hibernate.CompositeUserType and declare properties using the fully qualified classname of the type. Check out net.sf.hibernate.test.DoubleStringType to see the kind of things that are possible.

<property name="twoStrings" type="net.sf.hibernate.test.DoubleStringType">
    <column name="first_string"/>
    <column name="second_string"/>
</property>

Notice the use of <column> tags to map a property to multiple columns.

Even though Hibernate's rich range of built-in types and support for components means you will very rarely need to use a custom type, it is nevertheless considered good form to use custom types for (non-entity) classes that occur frequently in your application. For example, a MonetoryAmount class is a good candidate for a CompositeUserType, even though it could easily be mapped as a component. One motivation for this is abstraction. With a custom type, your mapping documents would be future-proofed against possible changes in your way of representing monetory values.

5.2.5. Any type mappings

There is one further type of property mapping. The <any> mapping element defines a polymorphic association to classes from multiple tables. This type of mapping always requires more than one column. The first column holds the type of the associated entity. The remaining columns hold the identifier. It is impossible to specify a foreign key constraint for this kind of association, so this is most certainly not meant as the usual way of mapping (polymorphic) associations. You should use this only in very special cases (eg. audit logs, user session data, etc).

<any name="anyEntity" id-type="long" meta-type="eg.custom.Class2TablenameType">
    <column name="table_name"/>
    <column name="id"/>
</any>

The meta-type attribute lets the application specify a custom type that maps database column values to persistent classes which have identifier properties of the type specified by id-type. If the meta-type returns instances of java.lang.Class, nothing else is required. On the other hand, if it is a basic type like string or character, you must specify the mapping from values to classes.

<any name="anyEntity" id-type="long" meta-type="string">
    <meta-value value="TBL_ANIMAL" class="Animal"/>
    <meta-value value="TBL_HUMAN" class="Human"/>
    <meta-value value="TBL_ALIEN" class="Alien"/>
    <column name="table_name"/>
    <column name="id"/>
</any>
<any
        name="propertyName"                      (1)
        id-type="idtypename"                     (2)
        meta-type="metatypename"                 (3)
        cascade="none|all|save-update"           (4)
        access="field|property|ClassName"        (5)
>
        <meta-value ... />
        <meta-value ... />
        .....
        <column .... />
        <column .... />
        .....
</any>
(1)

name: the property name.

(2)

id-type: the identifier type.

(3)

meta-type (optional - defaults to class): a type that maps java.lang.Class to a single database column or, alternatively, a type that is allowed for a discriminator mapping.

(4)

cascade (optional- defaults to none): the cascade style.

(5)

access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.

The old object type that filled a similar role in Hibernate 1.2 is still supported, but is now semi-deprecated.

5.3. SQL quoted identifiers

You may force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect (usually double quotes, but brackets for SQL Server and backticks for MySQL).

<class name="LineItem" table="`Line Item`">
    <id name="id" column="`Item Id`"/><generator class="assigned"/></id>
    <property name="itemNumber" column="`Item #`"/>
    ...
</class>

5.4. Modular mapping files

It is possible to define subclass and joined-subclass mappings in seperate mapping documents, directly beneath hibernate-mapping. This allows you to extend a class hierachy just by adding a new mapping file. You must specify an extends attribute in the subclass mapping, naming a previously mapped superclass. Use of this feature makes the ordering of the mapping documents important!

<hibernate-mapping>
        <subclass name="eg.subclass.DomesticCat" extends="eg.Cat" discriminator-value="D">
             <property name="name" type="string"/>
        </subclass>
</hibernate-mapping>