Mapping collections and entity associations Session - II
Difference between Entity &  Value Types Entity Has  Shared references Should have database identity No default transitive persistence Value Types It’s lifespan is bounded by the lifespan of the owning entity instance An object of value type has no database identity Has default transitive persistence.
Mapping of collections  (Value Types)
Example For Value Type Mapping
Mapping Value Type Mapping a single data type Usage: <set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;>   <key column=&quot;IT_ID&quot;/> <element type=&quot;string&quot; column=&quot;IMAGENAME&quot;/> </set> Mapping a Whole table using composite element. Usage: <set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;>    <key column=&quot;IT_ID&quot;/> <composite-element class=&quot;ImageDetails&quot;> <property name=&quot;imagename&quot; column=&quot;IMAGENAME&quot; not-null=&quot;true&quot;/>   <property name=&quot;imagLoc&quot; column=&quot;IMAGELOC&quot; not-null=&quot;true&quot;/> </composite-element> </set>
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&quot; table=&quot;ITEM_IMAGE&quot;>   <key column=&quot;ITEM_ID&quot;/>   <list-index column=&quot;POSITION&quot;/>   <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </list>
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&quot; table=&quot;ITEM_IMAGE&quot;>   <key column=&quot;ITEM_ID&quot;/> <element type=&quot;string&quot; column=“FILENAME&quot;/> </set> SortedSet <set name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot;  sort=&quot;natural&quot; >   <key column=&quot;ITEM_ID&quot;/>   <element type=&quot;string&quot; column=&quot;FILENAME&quot; /> </set>
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=&quot;images&quot; table=&quot;ITEM_IMAGES&quot;>   <key column=&quot;ITEM_ID&quot;/>   <map-key column=“IMAGENAME&quot; type=&quot;string&quot;/>   <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </map> SortedMap <map name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot; sort=&quot;natural&quot;>   <key column=&quot;ITEM_ID&quot;/>   <map-key column=&quot;IMAGENAME&quot; type=&quot;string&quot;/>   <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </map>
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=&quot;images&quot; table=&quot;ITEM_IDBAG_IMAGES&quot;>   <collection-id type=&quot;long&quot; column=“ITEM_IMAGE_ID&quot;>   <generator class=&quot;sequence&quot;/>   </collection-id>   <key column=&quot;ITEM_ID&quot;/>   <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </idbag>
Mapping of associations between entity classes (Entity) Scenario
One-to-many Each Item has a number of Bids Usage:- <set name=&quot;bids&quot; inverse=&quot;true&quot;  cascade=&quot;save-update,delete,delete-orphan&quot;> <key column=&quot;ITEM_ID&quot;/> <one-to-many class=&quot;Bid&quot;/> </set>
Many-to-one Many bids may point to a single Item Usage:- <many-to-one name=&quot;item“ column=&quot;ITEM_ID“ class=&quot;Item&quot;/> 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
Demo
Inheritance
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.
Table per concrete class with Union Scenario
Usages: <hibernate-mapping> <class name=&quot;BillingDetails“  abstract=&quot;true&quot; > <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;>   <generator class=&quot;native&quot;/> </id> <property name=&quot;name“ column=&quot;OWNER“ type=&quot;string&quot;/> <union-subclass name=&quot;CreditCard&quot; table=&quot;CREDIT_CARD&quot;> <property name=&quot;number&quot; column=”NUMBER”/> <property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </union-subclass> <union-subclass name=&quot;BankAccount&quot; table=&quot;BANK_ACCOUNT&quot;> ... </class> </hibernate-mapping>
Table per class hierarchy Scenario
Usage: <hibernate-mapping> <class name=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> <generator class=&quot;native&quot;/> </id> <discriminator column=&quot;BILLING_DETAILS_TYPE“ type=&quot;string&quot;/> <property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> ... <subclass name=&quot;CreditCard“ discriminator-value=&quot;CC&quot;> <property name=&quot;number&quot; column=&quot;CC_NUMBER&quot;/> <property name=&quot;expMonth&quot; column=&quot;CC_EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;CC_EXP_YEAR&quot;/> </subclass> <subclass name=”BankAccount” discriminator-value=”BA”>   ... </class> </hibernate-mapping>
Table per subclass Scenario
Usage <hibernate-mapping> <class name=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;>   <generator class=&quot;native&quot;/> </id> <property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> <joined-subclass name=&quot;CreditCard“ table=&quot;CREDIT_CARD&quot;> <key column=&quot;CREDIT_CARD_ID&quot;/> <property name=&quot;number&quot; column=&quot;NUMBER&quot;/> <property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </joined-subclass> <joined-subclass name=&quot;BankAccount“ table=&quot;BANK_ACCOUNT&quot;>   ... </class> </hibernate-mapping>
Demo

Hibernate Session 2

  • 1.
    Mapping collections andentity associations Session - II
  • 2.
    Difference between Entity& Value Types Entity Has Shared references Should have database identity No default transitive persistence Value Types It’s lifespan is bounded by the lifespan of the owning entity instance An object of value type has no database identity Has default transitive persistence.
  • 3.
    Mapping of collections (Value Types)
  • 4.
    Example For ValueType Mapping
  • 5.
    Mapping Value TypeMapping a single data type Usage: <set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;> <key column=&quot;IT_ID&quot;/> <element type=&quot;string&quot; column=&quot;IMAGENAME&quot;/> </set> Mapping a Whole table using composite element. Usage: <set name=&quot;images&quot; table=&quot;ITEM_SET_IMAGES&quot;> <key column=&quot;IT_ID&quot;/> <composite-element class=&quot;ImageDetails&quot;> <property name=&quot;imagename&quot; column=&quot;IMAGENAME&quot; not-null=&quot;true&quot;/> <property name=&quot;imagLoc&quot; column=&quot;IMAGELOC&quot; not-null=&quot;true&quot;/> </composite-element> </set>
  • 6.
    Different Choice ForValue 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&quot; table=&quot;ITEM_IMAGE&quot;> <key column=&quot;ITEM_ID&quot;/> <list-index column=&quot;POSITION&quot;/> <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </list>
  • 7.
    Sets :- Theorder 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&quot; table=&quot;ITEM_IMAGE&quot;> <key column=&quot;ITEM_ID&quot;/> <element type=&quot;string&quot; column=“FILENAME&quot;/> </set> SortedSet <set name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot; sort=&quot;natural&quot; > <key column=&quot;ITEM_ID&quot;/> <element type=&quot;string&quot; column=&quot;FILENAME&quot; /> </set>
  • 8.
    Maps :- Ajava.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=&quot;images&quot; table=&quot;ITEM_IMAGES&quot;> <key column=&quot;ITEM_ID&quot;/> <map-key column=“IMAGENAME&quot; type=&quot;string&quot;/> <element type=&quot;string&quot; column=“FILENAME&quot; not-null=&quot;true&quot;/> </map> SortedMap <map name=&quot;images&quot; table=&quot;ITEM_IMAGE&quot; sort=&quot;natural&quot;> <key column=&quot;ITEM_ID&quot;/> <map-key column=&quot;IMAGENAME&quot; type=&quot;string&quot;/> <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </map>
  • 9.
    Bags :- Possibleduplicates, 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=&quot;images&quot; table=&quot;ITEM_IDBAG_IMAGES&quot;> <collection-id type=&quot;long&quot; column=“ITEM_IMAGE_ID&quot;> <generator class=&quot;sequence&quot;/> </collection-id> <key column=&quot;ITEM_ID&quot;/> <element type=&quot;string&quot; column=&quot;FILENAME&quot; not-null=&quot;true&quot;/> </idbag>
  • 10.
    Mapping of associationsbetween entity classes (Entity) Scenario
  • 11.
    One-to-many Each Itemhas a number of Bids Usage:- <set name=&quot;bids&quot; inverse=&quot;true&quot; cascade=&quot;save-update,delete,delete-orphan&quot;> <key column=&quot;ITEM_ID&quot;/> <one-to-many class=&quot;Bid&quot;/> </set>
  • 12.
    Many-to-one Many bidsmay point to a single Item Usage:- <many-to-one name=&quot;item“ column=&quot;ITEM_ID“ class=&quot;Item&quot;/> 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
  • 13.
  • 14.
  • 15.
    Approaches Table perconcrete 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.
  • 16.
    Table per concreteclass with Union Scenario
  • 17.
    Usages: <hibernate-mapping> <classname=&quot;BillingDetails“ abstract=&quot;true&quot; > <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> <generator class=&quot;native&quot;/> </id> <property name=&quot;name“ column=&quot;OWNER“ type=&quot;string&quot;/> <union-subclass name=&quot;CreditCard&quot; table=&quot;CREDIT_CARD&quot;> <property name=&quot;number&quot; column=”NUMBER”/> <property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </union-subclass> <union-subclass name=&quot;BankAccount&quot; table=&quot;BANK_ACCOUNT&quot;> ... </class> </hibernate-mapping>
  • 18.
    Table per classhierarchy Scenario
  • 19.
    Usage: <hibernate-mapping> <classname=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> <generator class=&quot;native&quot;/> </id> <discriminator column=&quot;BILLING_DETAILS_TYPE“ type=&quot;string&quot;/> <property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> ... <subclass name=&quot;CreditCard“ discriminator-value=&quot;CC&quot;> <property name=&quot;number&quot; column=&quot;CC_NUMBER&quot;/> <property name=&quot;expMonth&quot; column=&quot;CC_EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;CC_EXP_YEAR&quot;/> </subclass> <subclass name=”BankAccount” discriminator-value=”BA”> ... </class> </hibernate-mapping>
  • 20.
  • 21.
    Usage <hibernate-mapping> <classname=&quot;BillingDetails“ table=&quot;BILLING_DETAILS&quot;> <id name=&quot;id“ column=&quot;BILLING_DETAILS_ID“ type=&quot;long&quot;> <generator class=&quot;native&quot;/> </id> <property name=&quot;owner“ column=&quot;OWNER“ type=&quot;string&quot;/> <joined-subclass name=&quot;CreditCard“ table=&quot;CREDIT_CARD&quot;> <key column=&quot;CREDIT_CARD_ID&quot;/> <property name=&quot;number&quot; column=&quot;NUMBER&quot;/> <property name=&quot;expMonth&quot; column=&quot;EXP_MONTH&quot;/> <property name=&quot;expYear&quot; column=&quot;EXP_YEAR&quot;/> </joined-subclass> <joined-subclass name=&quot;BankAccount“ table=&quot;BANK_ACCOUNT&quot;> ... </class> </hibernate-mapping>
  • 22.