Mapping collections and entity associations Session - II
Difference between Entity &  Value Types <ul><li>Entity </li></ul><ul><li>Has  Shared references </li></ul><ul><li>Should ...
Mapping of collections  (Value Types)
Example For Value Type Mapping
Mapping Value Type <ul><li>Mapping a single data type </li></ul><ul><li>Usage: </li></ul><ul><li><set name=&quot;images&qu...
Different Choice For Value Type  Collection Implementation  <ul><li>Lists </li></ul><ul><li>:- preserving the position of ...
<ul><li>Sets </li></ul><ul><li>:- The order of its elements isn’t preserved, and duplicate elements aren’t allowed. </li><...
<ul><li>Maps </li></ul><ul><li>:- A java.util.Map can be mapped with <map>, preserving key and value pairs.  </li></ul><ul...
<ul><li>Bags </li></ul><ul><li>:- Possible duplicates, no element order is preserved. </li></ul><ul><li>:- A java.util.Col...
Mapping of associations between entity classes (Entity) <ul><li>Scenario </li></ul>
One-to-many <ul><li>Each Item has a number of Bids </li></ul><ul><li>Usage:- </li></ul><ul><li><set name=&quot;bids&quot; ...
Many-to-one <ul><li>Many bids may point to a single Item </li></ul><ul><li>Usage:- </li></ul><ul><li><many-to-one name=&qu...
Demo
Inheritance
<ul><li>Approaches </li></ul><ul><li>Table per concrete class with implicit polymorphism   </li></ul><ul><ul><li>Use no ex...
Table per concrete class with Union <ul><li>Scenario </li></ul>
<ul><li>Usages: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“  abstract=&quot;...
Table per class hierarchy <ul><li>Scenario </li></ul>
<ul><li>Usage: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“ table=&quot;BILLI...
Table per subclass <ul><li>Scenario </li></ul>
<ul><li>Usage </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“ table=&quot;BILLIN...
Demo
Upcoming SlideShare
Loading in …5
×

Hibernate Session 2

1,364 views
1,284 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,364
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
70
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Hibernate Session 2

  1. 1. Mapping collections and entity associations Session - II
  2. 2. Difference between Entity & Value Types <ul><li>Entity </li></ul><ul><li>Has Shared references </li></ul><ul><li>Should have database identity </li></ul><ul><li>No default transitive persistence </li></ul><ul><li>Value Types </li></ul><ul><li>It’s lifespan is bounded by the lifespan of the owning entity instance </li></ul><ul><li>An object of value type has no database identity </li></ul><ul><li>Has default transitive persistence. </li></ul>
  3. 3. Mapping of collections (Value Types)
  4. 4. Example For Value Type Mapping
  5. 5. Mapping Value Type <ul><li>Mapping a single data type </li></ul><ul><li>Usage: </li></ul><ul><li><set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;> </li></ul><ul><li> <key column=&quot;IT_ID&quot;/> </li></ul><ul><li><element type=&quot;string&quot; column=&quot;IMAGENAME&quot;/> </li></ul><ul><li></set> </li></ul><ul><li>Mapping a Whole table using composite element. </li></ul><ul><li>Usage: </li></ul><ul><li><set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;> </li></ul><ul><li> <key column=&quot;IT_ID&quot;/> </li></ul><ul><li><composite-element class=&quot;ImageDetails&quot;> </li></ul><ul><li><property name=&quot;imagename&quot; column=&quot;IMAGENAME&quot; not-null=&quot;true&quot;/> </li></ul><ul><li> <property name=&quot;imagLoc&quot; column=&quot;IMAGELOC&quot; not-null=&quot;true&quot;/> </li></ul><ul><li></composite-element> </li></ul><ul><li></set> </li></ul>
  6. 6. Different Choice For Value Type Collection Implementation <ul><li>Lists </li></ul><ul><li>:- preserving the position of each element with an additional index column in the </li></ul><ul><li>collection table. </li></ul><ul><li>:- Initialize with a java.util.ArrayList for <list>. </li></ul><ul><li>Example:- </li></ul><ul><li>Usage:- </li></ul><ul><li><list name=“images&quot; table=&quot;ITEM_IMAGE&quot;> </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li> <list-index column=&quot;POSITION&quot;/> </li></ul><ul><li> <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </li></ul><ul><li></list> </li></ul>
  7. 7. <ul><li>Sets </li></ul><ul><li>:- The order of its elements isn’t preserved, and duplicate elements aren’t allowed. </li></ul><ul><li>:- Initialize with a java.util.HasSet for <set> </li></ul><ul><li>:- A java.util.SortedSet can be mapped with <set>, and the sort attribute </li></ul><ul><li>can be set to either a comparator or natural ordering for in-memory sorting. </li></ul><ul><li> Initialize the collection with a java.util.TreeSet instance. </li></ul><ul><li>Example:- </li></ul><ul><li>Usage:- </li></ul><ul><li><set name=“images&quot; table=&quot;ITEM_IMAGE&quot;> </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li><element type=&quot;string&quot; column=“FILENAME&quot;/> </li></ul><ul><li></set> </li></ul><ul><li>SortedSet </li></ul><ul><li><set name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot; sort=&quot;natural&quot; > </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li> <element type=&quot;string&quot; column=&quot;FILENAME&quot; /> </li></ul><ul><li></set> </li></ul>
  8. 8. <ul><li>Maps </li></ul><ul><li>:- A java.util.Map can be mapped with <map>, preserving key and value pairs. </li></ul><ul><li>Use a java.util.HashMap to initialize a property. </li></ul><ul><li>:-A java.util.SortedMap can be mapped with <map> element, and the sort </li></ul><ul><li> attribute can be set to either a comparator or natural ordering for in-memory </li></ul><ul><li> sorting. Initialize the collection with a java.util.TreeMap instance. </li></ul><ul><li>Example:- </li></ul><ul><li>Usage:- </li></ul><ul><li>< map name=&quot;images&quot; table=&quot;ITEM_IMAGES&quot;> </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li> <map-key column=“IMAGENAME&quot; type=&quot;string&quot;/> </li></ul><ul><li> <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </li></ul><ul><li></map> </li></ul><ul><li>SortedMap </li></ul><ul><li><map name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot; sort=&quot;natural&quot;> </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li> <map-key column=&quot;IMAGENAME&quot; type=&quot;string&quot;/> </li></ul><ul><li> <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </li></ul><ul><li></map> </li></ul>
  9. 9. <ul><li>Bags </li></ul><ul><li>:- Possible duplicates, no element order is preserved. </li></ul><ul><li>:- A java.util.Collection can be mapped with <bag> or <idbag>. </li></ul><ul><li>:- Hibernate supports persistent bags (it uses lists internally but ignores the </li></ul><ul><li>index of the elements). </li></ul><ul><li>:- Initialize with a java.util.ArrayList </li></ul><ul><li>Example :- </li></ul><ul><li>Usage :- </li></ul><ul><li><idbag name=&quot;images&quot; table=&quot;ITEM_IDBAG_IMAGES&quot;> </li></ul><ul><li> <collection-id type=&quot;long&quot; column=“ITEM_IMAGE_ID&quot;> </li></ul><ul><li> <generator class=&quot;sequence&quot;/> </li></ul><ul><li> </collection-id> </li></ul><ul><li> <key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li> <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </li></ul><ul><li></idbag> </li></ul>
  10. 10. Mapping of associations between entity classes (Entity) <ul><li>Scenario </li></ul>
  11. 11. One-to-many <ul><li>Each Item has a number of Bids </li></ul><ul><li>Usage:- </li></ul><ul><li><set name=&quot;bids&quot; inverse=&quot;true&quot; </li></ul><ul><li>cascade=&quot;save-update,delete,delete-orphan&quot;> </li></ul><ul><li><key column=&quot;ITEM_ID&quot;/> </li></ul><ul><li><one-to-many class=&quot;Bid&quot;/> </li></ul><ul><li></set> </li></ul>
  12. 12. Many-to-one <ul><li>Many bids may point to a single Item </li></ul><ul><li>Usage:- </li></ul><ul><li><many-to-one name=&quot;item“ column=&quot;ITEM_ID“ class=&quot;Item&quot;/> </li></ul><ul><li>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 </li></ul>
  13. 13. Demo
  14. 14. Inheritance
  15. 15. <ul><li>Approaches </li></ul><ul><li>Table per concrete class with implicit polymorphism </li></ul><ul><ul><li>Use no explicit inheritance mapping, and default runtime polymorphic behavior. </li></ul></ul><ul><li>Table per concrete class with Unions </li></ul><ul><ul><li>Discard polymorphism and inheritance relationships completely from the SQL schema. </li></ul></ul><ul><li>Table per class hierarchy </li></ul><ul><ul><li>Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information. </li></ul></ul><ul><li>Table per subclass </li></ul><ul><ul><li>Represent is a (inheritance) relationships as has a (foreign key) relationships. </li></ul></ul>
  16. 16. Table per concrete class with Union <ul><li>Scenario </li></ul>
  17. 17. <ul><li>Usages: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“ abstract=&quot;true&quot; > </li></ul><ul><ul><li><id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> </li></ul></ul><ul><ul><li> <generator class=&quot;native&quot;/> </li></ul></ul><ul><ul><li></id> </li></ul></ul><ul><ul><li><property name=&quot;name“ column=&quot;OWNER“ type=&quot;string&quot;/> </li></ul></ul><ul><ul><li><union-subclass name=&quot;CreditCard&quot; table=&quot;CREDIT_CARD&quot;> </li></ul></ul><ul><ul><li><property name=&quot;number&quot; column=”NUMBER”/> </li></ul></ul><ul><ul><li><property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> </li></ul></ul><ul><ul><li><property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </li></ul></ul><ul><ul><li></union-subclass> </li></ul></ul><ul><ul><li><union-subclass name=&quot;BankAccount&quot; table=&quot;BANK_ACCOUNT&quot;> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><li></class> </li></ul><ul><li></hibernate-mapping> </li></ul>
  18. 18. Table per class hierarchy <ul><li>Scenario </li></ul>
  19. 19. <ul><li>Usage: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> </li></ul><ul><ul><li><id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> </li></ul></ul><ul><ul><li><generator class=&quot;native&quot;/> </li></ul></ul><ul><ul><li></id> </li></ul></ul><ul><ul><li><discriminator column=&quot;BILLING_DETAILS_TYPE“ type=&quot;string&quot;/> </li></ul></ul><ul><ul><li><property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><subclass name=&quot;CreditCard“ discriminator-value=&quot;CC&quot;> </li></ul></ul><ul><ul><ul><li><property name=&quot;number&quot; column=&quot;CC_NUMBER&quot;/> </li></ul></ul></ul><ul><ul><ul><li><property name=&quot;expMonth&quot; column=&quot;CC_EXP_MONTH&quot;/> </li></ul></ul></ul><ul><ul><ul><li><property name=&quot;expYear&quot; column=&quot;CC_EXP_YEAR&quot;/> </li></ul></ul></ul><ul><ul><li></subclass> </li></ul></ul><ul><ul><li><subclass name=”BankAccount” discriminator-value=”BA”> </li></ul></ul><ul><ul><li> ... </li></ul></ul><ul><li></class> </li></ul><ul><li></hibernate-mapping> </li></ul>
  20. 20. Table per subclass <ul><li>Scenario </li></ul>
  21. 21. <ul><li>Usage </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> </li></ul><ul><ul><li><id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> </li></ul></ul><ul><ul><li> <generator class=&quot;native&quot;/> </li></ul></ul><ul><ul><li></id> </li></ul></ul><ul><ul><li><property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> </li></ul></ul><ul><ul><li><joined-subclass name=&quot;CreditCard“ table=&quot;CREDIT_CARD&quot;> </li></ul></ul><ul><ul><li><key column=&quot;CREDIT_CARD_ID&quot;/> </li></ul></ul><ul><ul><li><property name=&quot;number&quot; column=&quot;NUMBER&quot;/> </li></ul></ul><ul><ul><li><property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> </li></ul></ul><ul><ul><li><property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </li></ul></ul><ul><ul><li></joined-subclass> </li></ul></ul><ul><ul><li><joined-subclass name=&quot;BankAccount“ table=&quot;BANK_ACCOUNT&quot;> </li></ul></ul><ul><ul><li> ... </li></ul></ul><ul><li></class> </li></ul><ul><li></hibernate-mapping> </li></ul>
  22. 22. Demo

×