Click here to load reader
Upload
leonardo-passos
View
314
Download
4
Tags:
Embed Size (px)
DESCRIPTION
A description of how hibernate works.
Citation preview
2
Object Relational Mapping (ORM)
3
ORM library in Java
4
What is a mapping?
● Hibernate's MDE support:● Generation of corresponding DDL scripts● Generation of Java POJOs
<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" type="string" not-null="true"/> <property name="email" type="string" not-null="true"/> </class></hibernate-mapping>
5
Associations
● One to one
● One to many
● Many to many
● Unidirectional/Bidirectional
6
Some examples...
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" column="address_id" cascade="save-update" unique="true"/> </class></hibernate-mapping>
Needs mapping definition
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>
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>
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>
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></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" lazy="true" cascade="save-update"/> <key column="category_id"/> <many-to-many class="Item" column="item_id"/> </set> </class></hibernate-mapping>
1
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" lazy="true" cascade="save-update inverse=”true”"/> <key column="item_id"/> <many-to-many class="Category" column="category_id"/> </set> </class></hibernate-mapping>
2
14
Conclusions
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
16
Questions