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

Inheritance strategy

<discriminator

column="discriminator_column"

type="discriminator_type" force="true|false"

insert="true|false"

formula="arbitrary sql expression"

/>

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

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

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

insert (optional - defaults to true): set this to false if your discriminator column is also part of a mapped composite identifier. It tells Hibernate not to include the column in SQL INSERTs. formula (optional): an arbitrary SQL expression that is executed when a type has to be evaluated. It allows content-based discrimination.

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

The formula attribute allows you to declare an arbitrary SQL expression that will be used to evaluate the type of a row. For example:

<discriminator

formula="case when CLASS_TYPE in ('a', 'b', 'c') then 0 else 1 end"

type="integer"/>

5.1.6.2. Joined subclass strategy

Each subclass can also be mapped to its own table. This is called the table-per-subclass mapping strategy. An inherited state is retrieved by joining with the table of the superclass. A discriminator column is not required for this mapping strategy. Each subclass must, however, declare a table column holding the object identifier. The primary key of this table is also a foreign key to the superclass table and described by the @PrimaryKeyJoinColumns or the <key> element.

@Entity @Table(name="CATS")

@Inheritance(strategy=InheritanceType.JOINED)

public class Cat implements Serializable { @Id @GeneratedValue(generator="cat-uuid")

@GenericGenerator(name="cat-uuid", strategy="uuid") String getId() { return id; }

...

}

109

Chapter 5. Basic O/R Mapping

@Entity @Table(name="DOMESTIC_CATS")

@PrimaryKeyJoinColumn(name="CAT")

public class DomesticCat extends Cat {

public String getName() { return name; }

}

Note

The table name still defaults to the non qualified class name. Also if

@PrimaryKeyJoinColumn is not set, the primary key / foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass.

In hbm.xml, use the <joined-subclass> element. For example:

<joined-subclass

name="ClassName"

table="tablename"

proxy="ProxyInterface"

lazy="true|false" dynamic-update="true|false" dynamic-insert="true|false" schema="schema" catalog="catalog" extends="SuperclassName" persister="ClassName" subselect="SQL expression" entity-name="EntityName" node="element-name">

<key .... >

<property .... />

.....

</joined-subclass>

name: the fully qualified class name of the subclass. table: the name of the subclass table.

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

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

Use the <key> element to declare the primary key / foreign key column. The mapping at the start of the chapter would then be re-written as:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

110

Inheritance strategy

"-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.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>

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

5.1.6.3. Table per class strategy

A third option is to map only the concrete classes of an inheritance hierarchy to tables. This is called the table-per-concrete-class strategy. Each table defines all persistent states of the class, including the inherited state. In Hibernate, it is not necessary to explicitly map such inheritance hierarchies. You can map each class as a separate entity root. However, if you wish use polymorphic associations (e.g. an association to the superclass of your hierarchy), you need to use the union subclass mapping.

@Entity

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

public class Flight implements Serializable { ... }

Or in hbm.xml:

<union-subclass

name="ClassName"

table="tablename"

111

Chapter 5. Basic O/R Mapping

proxy="ProxyInterface"

lazy="true|false" dynamic-update="true|false" dynamic-insert="true|false" schema="schema" catalog="catalog" extends="SuperclassName" abstract="true|false" persister="ClassName" subselect="SQL expression" entity-name="EntityName" node="element-name">

<property .... />

.....

</union-subclass>

name: the fully qualified class name of the subclass. table: the name of the subclass table.

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

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

No discriminator column or key column is required for this mapping strategy.

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

5.1.6.4. Inherit properties from superclasses

This is sometimes useful to share common properties through a technical or a business superclass without including it as a regular mapped entity (ie no specific table for this entity). For that purpose you can map them as @MappedSuperclass.

@MappedSuperclass

public class BaseEntity {

@Basic

@Temporal(TemporalType.TIMESTAMP)

public Date getLastUpdate() { ... } public String getLastUpdater() { ... }

...

}

@Entity class Order extends BaseEntity { @Id public Integer getId() { ... }

...

}

In database, this hierarchy will be represented as an Order table having the id, lastUpdate and lastUpdater columns. The embedded superclass property mappings are copied into their entity subclasses. Remember that the embeddable superclass is not the root of the hierarchy though.

112

Inheritance strategy

Note

Properties from superclasses not mapped as @MappedSuperclass are ignored.

Note

The default access type (field or methods) is used, unless you use the @Access annotation.

Note

The same notion can be applied to @Embeddable objects to persist properties from their superclasses. You also need to use @MappedSuperclass to do that (this should not be considered as a standard EJB3 feature though)

Note

It is allowed to mark a class as @MappedSuperclass in the middle of the mapped inheritance hierarchy.

Note

Any class in the hierarchy non annotated with @MappedSuperclass nor @Entity will be ignored.

You can override columns defined in entity superclasses at the root entity level using the

@AttributeOverride annotation.

@MappedSuperclass

public class FlyingObject implements Serializable {

public int getAltitude() { return altitude;

}

@Transient

public int getMetricAltitude() { return metricAltitude;

}

@ManyToOne

public PropulsionType getPropulsion() {

113

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