Geeks With Blogs
Bob Palmer's Developer Blog .NET, SQL, and Silverlight Development

After a short break last week, I wanted to make sure I made time to publish the next in my series of tutorials on NHibernate.

This week I'll be covering Many to Many relationships, the hilo algorithm, IdBag element, and touch on Lazy Loading.

You can view the entire article at this link:

http://docs.google.com/Doc?docid=0AUP-rKyyUMKhZGczejdxeHZfMjZkdjd3cjJnMg&hl=en

As always, feedback and any technical bits I may have missed are always appreciated!

-Bob Palmer

Posted on Saturday, May 15, 2010 12:07 AM | Back to top


Comments on this post: NHibernate Tutorial #5 - Working with Many to Many relationships

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
I have enjoyed and appreciate the 5 tutorials thus far. Good job!

My only criticism is that I think the implementation of the repository pattern is sort of distracting. Besides that, I believe that by using a repository to abstract NHibernate, you are sacrificing NHibernate's flexibility and usefulness. For example, you had to manually initialize the categories collection within the repository method; doesn't that defeat the usefulness of lazy-loading?

Anyway, that's just my opinion. I am looking forward to the upcoming tutorials. Again, great job. You really are softening the "learning cliff" with your work.
Left by Ronnie Overby on Jun 03, 2010 9:58 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Hey Ronnie,

First - thanks for the feedback! I actually gave some thought to how to best introduce some patterns to the tuturials. There are actually a couple of reasons I generally use a repository pattern. The primary consideration is testability.

For example, I am currently working on a Kanban application that uses NHibernate for persistence, but when playing with the UI, I wanted to be able to experiment with some specific test cases without a dependency on a database, and without throwing away code.

By coding to a repository interface, I was able to knock together a simple mock object without an explicit dependency on NHibernate. This also lets me easily mock out my repository object to handle unit testing my controllers, etc.

RE Lazy loading, I would agree that in the example you might wonder what benefit we derived from using a repository. Of course, in the real world I would likely encapsulate transactions and much larger sections of behavior into a repository method, and within the contect of that method we could take advantage of the lazy loading features of NHibernate. Since most of my work is web based, I generally do not find a necessity to persist a session over more than one method call, so this pattern works out pretty well in that circumstance.

There are also some really cool things you can do with abstraction, like dynamic search criteria, etc. (all abstracted from the persistence layer) that I plan on showing a few tutorials down the line.

Thanks again, and glad you are finding the articles useful!
Left by Bob Palmer on Jun 08, 2010 9:26 PM

# re: NHibernate Tutorial #2 -
Requesting Gravatar...
Hi Bob Palmer!

I have read your second tutorial of nhibernate and got an exception while running that console application. Then exception occurs while save the object. Exception says:

No persister for: ACME.Model.Product

Any suggesions?

Left by Naveed Anjum on Jun 10, 2010 8:22 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Quick things to check:
- Does the app.config use a valid connection string for your SQL database?
- When you created your *.hbm.xml files, did you ensure that the build action was set to 'Embedded REsource'?

Those are a couple of the most common ones. What you can do as well when you get that exception is set a breakpoint, and while in the debugger take a look at the InnerException property - this can provide a ton of really good info.

Let me know if that works out for you, if not let me know (and if possible let me know what the inner exception is).

Thanks
Bob
Left by Bob Palmer on Jun 10, 2010 8:27 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Thanks for the quick reply!

I haven,t set the build action as Embeded Resource. Now it gets working for me. Again thank you so much for providing such a brilliant tutorial. I am looking forward for more... God Bless you.
Left by Naveed Anjum on Jun 10, 2010 8:33 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Not a problem - glad I could help :) The Embedded Resource piece is a pretty common thing and usually one of the first things I check with that error.
Left by Bob Palmer on Jun 10, 2010 8:56 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Is it wrong making a many to many relation bidirectional... I mean if I create a second many to many from Categories to Products

Would be enough replicate the product many-to-many in the category mapping?
Left by Simon on Aug 14, 2011 9:27 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Thanks so much for these tutorials Bob, I have struggled with NHibernate in the past, and you have made it straightforward. Excellent tutorials. Thanks again :)
Left by Louise on Oct 13, 2011 5:25 AM

# re: NHibernate Tutorial #5 - Working with Many to Many relationships
Requesting Gravatar...
Hi,

This is what I am looking for. Its cut down my time for on-to-many and many-to-one.

Thanks for sharing.
Left by Quang on Mar 01, 2012 9:30 AM

Your comment:
 (will show your gravatar)


Copyright © BobPalmer | Powered by: GeeksWithBlogs.net