hibernate session 2

22
Mapping collections and entity associations Session - II

Upload: bkathir

Post on 18-May-2015

1.210 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Hibernate Session 2

Mapping collectionsand entity associations

Session - II

Page 2: Hibernate Session 2

Difference between Entity & Value Types

Entity

1. Has Shared references

2. Should have database identity

3. No default transitive persistence

Value Types

1. It’s lifespan is bounded by the lifespan of the owning entity instance

2. An object of value type has no database identity

3. Has default transitive persistence.

Page 3: Hibernate Session 2

Mapping of collections (Value Types)

Page 4: Hibernate Session 2

Example For Value Type Mapping

Page 5: Hibernate Session 2

Mapping Value Type• Mapping a single data type Usage:

<set name="images" table="ITEM_SET_IMAGES"> <key column="IT_ID"/>

<element type="string" column="IMAGENAME"/> </set>

• Mapping a Whole table using composite element. Usage:

<set name="images" table="ITEM_SET_IMAGES"> <key column="IT_ID"/> <composite-element class="ImageDetails"> <property name="imagename" column="IMAGENAME" not-

null="true"/> <property name="imagLoc" column="IMAGELOC" not-null="true"/>

</composite-element> </set>

Page 6: Hibernate Session 2

Different Choice For Value Type Collection Implementation

• Lists :- preserving the position of each element with an additional index column in the collection table. :- Initialize with a java.util.ArrayList for <list>.

Example:-

Usage:-<list name=“images" table="ITEM_IMAGE"> <key column="ITEM_ID"/> <list-index column="POSITION"/> <element type="string" column=“FILENAME" not-null="true"/></list>

Page 7: Hibernate Session 2

• Sets :- The order of its elements isn’t preserved, and duplicate elements aren’t allowed. :- Initialize with a java.util.HasSet for <set> :- A java.util.SortedSet can be mapped with <set>, and the sort attribute can be set to either a comparator or natural ordering for in-memory sorting.

Initialize the collection with a java.util.TreeSet instance.

Example:-

Usage:-<set name=“images" table="ITEM_IMAGE"> <key column="ITEM_ID"/>

<element type="string" column=“FILENAME"/></set>

SortedSet

<set name="images" table="ITEM_IMAGE" sort="natural"> <key column="ITEM_ID"/> <element type="string" column="FILENAME" />

</set>

Page 8: Hibernate Session 2

• Maps :- A java.util.Map can be mapped with <map>, preserving key and value pairs.

Use a java.util.HashMap to initialize a property.

:-A java.util.SortedMap can be mapped with <map> element, and the sort

attribute can be set to either a comparator or natural ordering for in-memory

sorting. Initialize the collection with a java.util.TreeMap instance.

Example:-

Usage:-

<map name="images" table="ITEM_IMAGES">

<key column="ITEM_ID"/>

<map-key column=“IMAGENAME" type="string"/>

<element type="string" column=“FILENAME" not-null="true"/>

</map>

SortedMap

<map name="images" table="ITEM_IMAGE" sort="natural">

<key column="ITEM_ID"/>

<map-key column="IMAGENAME" type="string"/>

<element type="string" column="FILENAME" not-null="true"/>

</map>

Page 9: Hibernate Session 2

• Bags :- Possible duplicates, no element order is preserved. :- A java.util.Collection can be mapped with <bag> or <idbag>. :- Hibernate supports persistent bags (it uses lists internally but ignores the index of the elements). :- Initialize with a java.util.ArrayList

Example:-

Usage:- <idbag name="images" table="ITEM_IDBAG_IMAGES">

<collection-id type="long" column=“ITEM_IMAGE_ID"> <generator class="sequence"/> </collection-id> <key column="ITEM_ID"/> <element type="string" column="FILENAME" not-null="true"/>

</idbag>

Page 10: Hibernate Session 2

Mapping of associations between entity classes (Entity)

• Scenario

Page 11: Hibernate Session 2

One-to-many

• Each Item has a number of Bids

Usage:-

<set name="bids" inverse="true"

cascade="save-update,delete,delete-orphan">

<key column="ITEM_ID"/>

<one-to-many class="Bid"/>

</set>

Page 12: Hibernate Session 2

Many-to-one

• Many bids may point to a single Item

Usage:-

<many-to-one name="item“ column="ITEM_ID“ class="Item"/>

This indicates that the collection contains not value type instances, but references to entity instances. Hibernate now knows how to treat shared references and the lifecycle of the associated objects

Page 13: Hibernate Session 2

Demo

Page 14: Hibernate Session 2

Inheritance

Page 15: Hibernate Session 2

Approaches

• Table per concrete class with implicit polymorphism • Use no explicit inheritance mapping, and default runtime polymorphic

behavior.

• Table per concrete class with Unions• Discard polymorphism and inheritance relationships completely from the

SQL schema.

• Table per class hierarchy• Enable polymorphism by denormalizing the SQL schema, and utilize a

type discriminator column that holds type information.

• Table per subclass• Represent is a (inheritance) relationships as has a (foreign key)

relationships.

Page 16: Hibernate Session 2

Table per concrete class with Union

• Scenario

Page 17: Hibernate Session 2

• Usages:

<hibernate-mapping><class name="BillingDetails“ abstract="true">

<id name="id“ column="BILLING_DETAILS_ID“ type="long"> <generator class="native"/>

</id>

<property name="name“ column="OWNER“ type="string"/>

<union-subclass name="CreditCard" table="CREDIT_CARD"><property name="number" column=”NUMBER”/><property name="expMonth" column="EXP_MONTH"/><property name="expYear" column="EXP_YEAR"/>

</union-subclass>

<union-subclass name="BankAccount" table="BANK_ACCOUNT">...

</class></hibernate-mapping>

Page 18: Hibernate Session 2

Table per class hierarchy

• Scenario

Page 19: Hibernate Session 2

• Usage:<hibernate-mapping>

<class name="BillingDetails“ table="BILLING_DETAILS">

<id name="id“ column="BILLING_DETAILS_ID“ type="long">

<generator class="native"/>

</id>

<discriminator column="BILLING_DETAILS_TYPE“ type="string"/>

<property name="owner“ column="OWNER“ type="string"/>

...

<subclass name="CreditCard“ discriminator-value="CC">

<property name="number" column="CC_NUMBER"/>

<property name="expMonth" column="CC_EXP_MONTH"/>

<property name="expYear" column="CC_EXP_YEAR"/>

</subclass>

<subclass name=”BankAccount” discriminator-value=”BA”>

...

</class>

</hibernate-mapping>

Page 20: Hibernate Session 2

Table per subclass• Scenario

Page 21: Hibernate Session 2

• Usage<hibernate-mapping>

<class name="BillingDetails“ table="BILLING_DETAILS">

<id name="id“ column="BILLING_DETAILS_ID“ type="long">

<generator class="native"/>

</id>

<property name="owner“ column="OWNER“ type="string"/>

<joined-subclass name="CreditCard“ table="CREDIT_CARD">

<key column="CREDIT_CARD_ID"/>

<property name="number" column="NUMBER"/>

<property name="expMonth" column="EXP_MONTH"/>

<property name="expYear" column="EXP_YEAR"/>

</joined-subclass>

<joined-subclass name="BankAccount“ table="BANK_ACCOUNT">

...

</class>

</hibernate-mapping>

Page 22: Hibernate Session 2

Demo