Associating With NHibernate Object Relational Mapper For 'Net - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Associating With NHibernate Object Relational Mapper For 'Net

Description:

access='NHibernate.Generics.GenericAccessor ... Associations Many To One ... Owning the collection - one of the sides of the collection is 'responsible' to ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 13
Provided by: oren4
Category:

less

Transcript and Presenter's Notes

Title: Associating With NHibernate Object Relational Mapper For 'Net


1
Associating With NHibernateObject Relational
MapperFor .Net
  • Oren Eini
  • Orene_at_we-can.co.il

2
Associations - One To Many
blog.Posts
  • ltset name'Posts'
  • access'NHibernate.Generics.GenericAccessorCamel
    Case, NHibernate.Generics'
  • lazy'true' cascade'all-delete-orphan'
    inverse'true'gt
  • ltkey column'post_blogid' /gt
  • ltone-to-many class'NHibernate.Generics.Tests.Pos
    t, NHibernate.Generics.Tests' /gt
  • lt/setgt

3
Associations Many To One
post.Blog
  • ltmany-to-one name'Blog' column'post_blogid'
    access'NHibernate.Generics.GenericAccessor,
    NHibernate.Genericsclass'NHibernate.Generics.Te
    sts.Blog, NHibernate.Generics.Tests' /gt

4
Associations Many to Many
  • ltset name'Users' access'NHibernate.Generics.Gene
    ricAccessor, NHibernate.Generics'
  • table'UsersBlogs' lazy'true'
    cascade'save-update' inverse'true'gt
  • ltkey column'blog_id' /gt
  • ltmany-to-many column'user_id'
  • class'NHibernate.Generics.Tests.User,
    NHibernate.Generics.Tests'/gt
  • lt/setgt

5
Two way associations
  • Maintaining Integrity Of Associations
  • blog.Posts.Add(post)
  • post.Blog blog
  • Smart Collections (NHibernate.Generics)
  • posts new EntitySetltPostgt(delegate(Post p)
    p.Blog this ,delegate(Post p) p.Blog
    null )
  • blog new EntityRefltBloggt(delegate(Tests.Blog
    b) b.Posts.Add(this) ,delegate(Tests.Blog b)
    b.Posts.Remove(this))

6
NHibernate Collections
  • Set (ISet)
  • Most common collection for database.
  • No duplicates
  • No indexer
  • List (IList)
  • Required some sort of indexer in database
  • Meaningful Order
  • Bag (IList)
  • Allows duplicates
  • Has indexer
  • Not recommended in general

7
NHibernate Collections - Continued
  • Map (IDictionary)
  • Key / Value Pair
  • Can use entities / value types / components as
    keys / values
  • All Collections can be ordered / unordered
  • Recommended Make collections lazy to avoid
    loading too much, too often.
  • Advanced
  • Collections could be filtered
  • Custom Collections

8
Collections Lazy Loading
  • Collections are non-lazy by default.
  • Without Lazy Load - Problem All The Database Is
    In Memory
  • When you load an object, all its collections are
    loaded, and all their references are loaded, and
    all their collections, etc.
  • With Lazy Load - Problem Select N 1
  • Load a set object, then access their collections,
    issue a select per object in the set.
  • Both problems can be mitigated by selective use
    of lazy loading and eager fetching.

9
Collection Ownership
  • Owning the collection - one of the sides of the
    collection is responsible to save the
    association to the database.
  • When the association is one to many, the
    responsibility is usually on the many side, since
    the value is on its table.
  • When the association in many to many, one of the
    sides need to be chosen. This is a policy
    decision. Saving the other side after adding an
    association will not update the database.

10
Cascades
  • NHibernate cascades operate similarly to database
    FK constraints, and serve the same purpose.
  • Cascade Options
  • None
  • Save-Update
  • All
  • Delete
  • Delete-Orphans
  • All-Delete-Orphans
  • Note Correct cascades are crucial for the
    application.

11
A Few Words Of Advise
  • Doing blog.Posts.Count causes NHibernate to
    load the whole collection to memory (if lazy).
    Wasteful in resources. Use HQL for this.
  • Eager fetch if you need, try to limit use of ad
    hoc lazy loading if possible.
  • The session that loaded the object is tied to its
    lazy load behavior, if the session is closed and
    a lazy loaded collection is accessed, an
    exception is thrown.
  • Think carefully about lazy loading and cascades,
    changes in those can cause cascading affects to
    the application (pun intended).

12
Questions Comments
Write a Comment
User Comments (0)
About PowerShow.com