Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hibernate

358 views

Published on

A description of how hibernate works.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Hibernate

  1. 1. MDE using Hibernate ORM Leonardo Passos (lpassos@gsd.uwaterloo.ca) 1
  2. 2. Object Relational Mapping (ORM) 2
  3. 3. ORM library in Java 3
  4. 4. What is a mapping? <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>● Hibernates MDE support: ● Generation of corresponding DDL scripts ● Generation of Java POJOs 4
  5. 5. Associations● One to one● One to many● Many to many● Unidirectional/Bidirectional 5
  6. 6. Some examples... 6
  7. 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. 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. 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. 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. 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. 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. 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
  14. 14. Conclusions 14
  15. 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
  16. 16. Questions 16

×