Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
54
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

Chapter 5. Basic O/R Mapping

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

lazy (optional - defaults to false): specifies that this component should be fetched lazily when the instance variable is first accessed. It requires build-time bytecode instrumentation. optimistic-lock (optional - defaults to true): specifies that updates to this component either do or do not require acquisition of the optimistic lock. It determines if a version increment should occur when this property is dirty.

unique (optional - defaults to false): specifies that a unique constraint exists upon all mapped columns of the component.

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. See Section 9.5, “Dynamic components” for more information. This feature is not supported in annotations.

5.1.6. Inheritance strategy

Java is a language supporting polymorphism: a class can inherit from another. Several strategies are possible to persist a class hierarchy:

Single table per class hierarchy strategy: a single table hosts all the instances of a class hierarchy

Joined subclass strategy: one table per class and subclass is present and each table persist the properties specific to a given subclass. The state of the entity is then stored in its corresponding class table and all its superclasses

Table per class strategy: one table per concrete class and subclass is present and each table persist the properties of the class and its superclasses. The state of the entity is then stored entirely in the dedicated table for its class.

5.1.6.1. Single table per class hierarchy strategy

With this approach the properties of all the subclasses in a given mapped class hierarchy are stored in a single table.

Each subclass declares its own persistent properties and subclasses. Version and id properties are assumed to be inherited from the root class. Each subclass in a hierarchy must define a unique discriminator value. If this is not specified, the fully qualified Java class name is used.

@Entity

106

Inheritance strategy

@Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(

name="planetype", discriminatorType=DiscriminatorType.STRING

)

@DiscriminatorValue("Plane") public class Plane { ... }

@Entity

@DiscriminatorValue("A320")

public class A320 extends Plane { ... }

In hbm.xml, for the table-per-class-hierarchy mapping strategy, the <subclass> declaration is

used. For example:

<subclass

name="ClassName"

discriminator-value="discriminator_value"

proxy="ProxyInterface"

lazy="true|false" dynamic-update="true|false" dynamic-insert="true|false" entity-name="EntityName" node="element-name" extends="SuperclassName">

<property .... />

.....

</subclass>

name: the fully qualified class name of the subclass.

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

proxy (optional): specifies a class or interface used for lazy initializing proxies.

lazy (optional - defaults to true): setting lazy="false" disables the use of lazy fetching.

For information about inheritance mappings see Chapter 10, Inheritance mapping.

5.1.6.1.1. Discriminator

Discriminators are required for polymorphic persistence using the table-per-class-hierarchy mapping strategy. It 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. Hibernate Core supports the follwoing restricted set of types as discriminator column: string, character, integer, byte, short, boolean, yes_no, true_false.

Use the @DiscriminatorColumn to define the discriminator column as well as the discriminator

type.

107

Chapter 5. Basic O/R Mapping

Note

The enum DiscriminatorType used in

javax.persitence.DiscriminatorColumn only contains the values STRING,

CHAR and INTEGER which means that not all Hibernate supported types are available via the @DiscriminatorColumn annotation.

You can also use @DiscriminatorFormula to express in SQL a virtual discriminator column. This is particularly useful when the discriminator value can be extracted from one or more columns of the table. Both @DiscriminatorColumn and @DiscriminatorFormula are to be set on the root entity (once per persisted hierarchy).

@org.hibernate.annotations.DiscriminatorOptions allows to optionally specify Hibernate specific discriminator options which are not standardized in JPA. The available options are force and insert. The force attribute is useful if the table contains rows with "extra" discriminator values that are not mapped to a persistent class. This could for example occur when working with a legacy database. If force is set to true Hibernate will specify the allowed discriminator values in the SELECT query, even when retrieving all instances of the root class. The second option - insert

- tells Hibernate whether or not to include the discriminator column in SQL INSERTs. Usually the column should be part of the INSERT statement, but if your discriminator column is also part of a mapped composite identifier you have to set this option to false.

Tip

There is also a @org.hibernate.annotations.ForceDiscriminator annotation which is deprecated since version 3.6. Use @DiscriminatorOptions instead.

Finally, use @DiscriminatorValue on each class of the hierarchy to specify the value stored in the discriminator column for a given entity. If you do not set @DiscriminatorValue on a class, the fully qualified class name is used.

@Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(

name="planetype", discriminatorType=DiscriminatorType.STRING

)

@DiscriminatorValue("Plane") public class Plane { ... }

@Entity

@DiscriminatorValue("A320")

public class A320 extends Plane { ... }

In hbm.xml, the <discriminator> element is used to define the discriminator column or formula:

108

Соседние файлы в папке more