• Like
Hibernate Session 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Hibernate Session 2

  • 1,177 views
Published

 

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,177
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
68
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Mapping collections and entity 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 Value Type Mapping
  • 5. 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>
  • 6. 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>
  • 7.
    • 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>
  • 8.
    • 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>
  • 9.
    • 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>
  • 10. Mapping of associations between entity classes (Entity)
    • Scenario
  • 11. 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>
  • 12. 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
  • 13. Demo
  • 14. Inheritance
  • 15.
    • 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.
  • 16. Table per concrete class with Union
    • Scenario
  • 17.
    • 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>
  • 18. Table per class hierarchy
    • Scenario
  • 19.
    • 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>
  • 20. Table per subclass
    • Scenario
  • 21.
    • 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>
  • 22. Demo