This document discusses using Hibernate ORM for object-relational mapping in Java. It describes what ORM is and how Hibernate provides a mapping specification to define associations between objects and database tables. The mapping is defined declaratively in XML and Hibernate supports generation of database schemas and Java POJOs from these mappings. Examples are provided of common association types like one-to-one, one-to-many, and many-to-many defined between mapped classes. Bidirectional and unidirectional mappings are demonstrated. The conclusions emphasize that the mapping specification defines the semantics of associations and allows modeling in a declarative way that Hibernate can use for code generation tasks.
7. Unidirectional one to one association
<hibernate-mapping>
<class name="sample.Person" table="person">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="string" not-null="true"/>
<property name="phoneNumber" column=”phone_number” type="string" not-null="true"/>
<property name="email" type="string" not-null="true"/>
<many-to-one name="address"
class="Address" Needs mapping definition
column="address_id"
cascade="save-update"
unique="true"/>
</class>
</hibernate-mapping>
7
Solution: foreign key association
8. Unidirectional one to one association
<hibernate-mapping>
<class name="sample.Address" table="address">
<id name="id">
<generator class="native"/>
</id>
<property name="street" type="string" not-null="true"/>
<property name="zipcode" type="string" not-null="true"/>
<!-- other properties of interest... -->
</class>
</hibernate-mapping>
8
9. Unidirectional one to one association (stronger)
<hibernate-mapping>
<class name="sample.Person" table="person">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="string" not-null="true"/>
<property name="phoneNumber" column=”phone_number” type="string" not-null="true"/>
<property name="email" type="string" not-null="true"/>
<many-to-one name="address" class="Address"
column="address_id"
cascade="all"
unique="true"/>
</class>
</hibernate-mapping>
9
10. Bidirectional one to one association
● Person defined as before, but Address now contains
<hibernate-mapping>
<class name="sample.Address" table="address">
<id name="id">
<generator class="native"/>
</id>
<property name="street" type="string" not-null="true"/>
<property name="zipcode" type="string" not-null="true"/>
<!-- other properties of interest... -->
<one-to-one name="person"
class="sample.Person"
property-ref="address"/>
</class>
</hibernate-mapping>
10
11. Bidirectional one to one association
(alternative solution)
<hibernate-mapping>
<class name="sample.Person" table="person">
<id name="id">
<generator class="native"/>
</id>
<!-- properties... -->
<one-to-one name="address" class="sample.Address" cascade="all" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="sample.Address" table="address">
<id name="id">
<generator class="foreign">
<param name=”property”> person </param>
</id>
<!-- properties... -->
<one-to-one name=”person” class=”sample.Person” constrained=”true”/>
</class>
11
</hibernate-mapping>
Solution: primary key association
12. Bidirectional many to many association
● Suppose two classes: Category and Item
● A bidirectional association from Category to Item ('*' on both ends)
– In RDB, one needs to use an intermediate table to connect category and item
(e.g.: category_item)
<hibernate-mapping>
<class name="sample.Category" table="category">
...
<set name="items" table="category_item" 1
lazy="true"
cascade="save-update"/>
<key column="category_id"/>
<many-to-many class="Item" column="item_id"/>
</set>
</class>
</hibernate-mapping>
12
13. Bidirectional many to many association
● Suppose two classes: Category and Item
● A bidirectional association from Category to Item ('*' on both ends)
– In RDB, one needs to use an intermediate table to connect category and item
(e.g.: category_item)
<hibernate-mapping>
<class name="sample.Item" table="item">
...
<set name="categories" table="category_item" 2
lazy="true"
cascade="save-update
inverse=”true”"/>
<key column="item_id"/>
<many-to-many class="Category" column="category_id"/>
</set>
</class>
</hibernate-mapping>
13
15. Conclusions
● The semantics of associations are defined as part of the
mapping specification
● Users give the semantics they need
● Models are written in a declarative-fashion using XML
● MDE Support:
● DDL generation
● POJO generation
15