• No results found

Objects dépendants

8. Mapping de composants

8.1. Objects dépendants

A component is a contained object that is persisted as a value type and not an entity reference. The term "component" refers to the object-oriented notion of composition and not to architecture- level components. For example, you can model a person like this:

public class Person {

private java.util.Date birthday; private Name name;

private String key; public String getKey() { return key;

}

private void setKey(String key) { this.key=key;

}

public java.util.Date getBirthday() { return birthday;

}

public void setBirthday(java.util.Date birthday) { this.birthday = birthday;

}

public Name getName() { return name;

}

public void setName(Name name) { this.name = name;

} ... ... }

public class Name { char initial; String first; String last;

}

void setFirst(String first) { this.first = first;

}

public String getLast() { return last;

}

void setLast(String last) { this.last = last;

}

public char getInitial() { return initial;

}

void setInitial(char initial) { this.initial = initial; }

}

Now Name can be persisted as a component of Person. Name defines getter and setter methods

for its persistent properties, but it does not need to declare any interfaces or identifier properties.

Our Hibernate mapping would look like this:

<class name="eg.Person" table="person"> <id name="Key" column="pid" type="string"> <generator class="uuid"/>

</id>

<property name="birthday" type="date"/>

<component name="Name" class="eg.Name"> <!-- class attribute optional --> <property name="initial"/>

<property name="first"/> <property name="last"/> </component>

</class>

La table person aurai les colonnes pid, birthday, initial, first and last.

Like value types, components do not support shared references. In other words, two persons could have the same name, but the two person objects would contain two independent name objects that were only "the same" by value. The null value semantics of a component are ad hoc. When reloading the containing object, Hibernate will assume that if all component columns are null, then the entire component is null. This is suitable for most purposes.

Collection d'objets dépendants

The properties of a component can be of any Hibernate type (collections, many-to-one associations, other components, etc). Nested components should not be considered an exotic usage. Hibernate is intended to support a fine-grained object model.

Le <component> peut inclure dans la liste de ses propriétés une référence au <parent> conteneur.

<class name="eg.Person" table="person"> <id name="Key" column="pid" type="string"> <generator class="uuid"/>

</id>

<property name="birthday" type="date"/>

<component name="Name" class="eg.Name" unique="true">

<parent name="namedPerson"/> <!-- reference back to the Person --> <property name="initial"/>

<property name="first"/> <property name="last"/> </component>

</class>

8.2. Collection d'objets dépendants

Collections of components are supported (e.g. an array of type Name). Declare your component

collection by replacing the <element> tag with a <composite-element> tag:

<set name="someNames" table="some_names" lazy="true"> <key column="id"/>

<composite-element class="eg.Name"> <!-- class attribute required --> <property name="initial"/> <property name="first"/> <property name="last"/> </composite-element> </set>

Important

If you define a Set of composite elements, it is important to implement equals()

and hashCode() correctly.

Composite elements can contain components but not collections. If your composite element contains components, use the <nested-composite-element> tag. This case is a collection of components which themselves have components. You may want to consider if a one-to-many

even though the Java model is the same, the relational model and persistence semantics are still slightly different.

A composite element mapping does not support null-able properties if you are using a <set>. There is no separate primary key column in the composite element table. Hibernate uses each column's value to identify a record when deleting objects, which is not possible with null values. You have to either use only not-null properties in a composite-element or choose a <list>, <map>,

<bag> or <idbag>.

A special case of a composite element is a composite element with a nested <many-to-one>

element. This mapping allows you to map extra columns of a many-to-many association table to the composite element class. The following is a many-to-many association from Order to Item, where purchaseDate, price and quantity are properties of the association:

<class name="eg.Order" .... > ....

<set name="purchasedItems" table="purchase_items" lazy="true"> <key column="order_id">

<composite-element class="eg.Purchase"> <property name="purchaseDate"/> <property name="price"/>

<property name="quantity"/>

<many-to-one name="item" class="eg.Item"/> <!-- class attribute is optional --> </composite-element>

</set> </class>

There cannot be a reference to the purchase on the other side for bidirectional association navigation. Components are value types and do not allow shared references. A single Purchase

can be in the set of an Order, but it cannot be referenced by the Item at the same time.

Même les associations ternaires ou quaternaires sont possibles:

<class name="eg.Order" .... > ....

<set name="purchasedItems" table="purchase_items" lazy="true"> <key column="order_id">

<composite-element class="eg.OrderLine">

<many-to-one name="purchaseDetails class="eg.Purchase"/> <many-to-one name="item" class="eg.Item"/>

</composite-element> </set>

Utiliser les composants comme index de map

</class>

Composite elements can appear in queries using the same syntax as associations to other entities.

Related documents