Fluent Nhibernate Mapping - Best way to map many to many reference table

2,084 views
Skip to first unread message

Thakkar

unread,
Oct 19, 2009, 4:50:26 PM10/19/09
to Fluent NHibernate
What is the best way to map reference table in many to many
relationship?

I have these three tables.
CustomerOrders
--------------------
ID [Primary Key]
Name

OrderItems [Reference table]
----------------------
ID [Primary Key]
OrderID
ProductID
Quantity

Products
---------------------
ID [Primary Key]
Name

How do I map OrderItems? I have seen examples where the reference
table has composite key.

James Gregory

unread,
Oct 19, 2009, 5:38:39 PM10/19/09
to fluent-n...@googlegroups.com
In NHibernate's eyes your many-to-many isn't a true many-to-many relationship due to the intrim table having additional data, effectively making it another entity. You need to create an entity for an OrderItem, with a one-to-many on CustomerOrder and Product both pointing to OrderItem, and OrderItem should have a many-to-one back to each.

Thakkar

unread,
Oct 19, 2009, 6:38:51 PM10/19/09
to Fluent NHibernate
Thanks for your feedback.

I will implement mapping class for each many-to-many reference tables.

Hudson Akridge

unread,
Oct 19, 2009, 6:42:02 PM10/19/09
to fluent-n...@googlegroups.com
That's not exactly what the solution is all the time. In Orderlines it was, because it contained additional information, than just which orders had which products (and that would be the quantity). In many cases you can get away with not having to define an actual model class for your many-to-many table.

For example, If product can appear in many Categories, and Categories can have many products, then you've got a good use for a many-to-many element mapping. You don't need that join table represented in the model in any capacity, you just need the references between the two tables tracked. You would have a many-to-many table with just two columns. ProductID and CategoryID.

Mohamed Meligy

unread,
Oct 20, 2009, 1:32:13 AM10/20/09
to fluent-n...@googlegroups.com
In the case above (let me quote it):

> > CustomerOrders
> > --------------------
> > ID [Primary Key]
> > Name
>
> > OrderItems [Reference table]
> > ----------------------
> > ID [Primary Key]
> > OrderID
> > ProductID
> > Quantity
>
> > Products
> > ---------------------
> > ID [Primary Key]
> > Name

Assuming "OrderItem" is treated as a separate entity. For an OrderItem there will be a composite ID consisting of 2 Key References (Order, Product).

What if -for some reason- I want to cascade update SaveUpdate those 2 key references? How would I do that???


Regards,

--
Mohamed Meligy
Information Analyst (.Net Technologies) – Applications Delivery - TDG
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624

E-mail: eng.m...@gmail.com
Weblog: http://weblogs.asp.net/meligy

Hudson Akridge

unread,
Oct 20, 2009, 8:14:12 AM10/20/09
to fluent-n...@googlegroups.com
If you didn't have a PK in the Orderline table, and it's being treated as an entity, with two composite keys, I believe the answer to that question is you cascade like normal. It's in your equality overrides that you'd perform the check saying that if the Order.Id and Product.Id were the same, then the Orderline was the same.

Mohamed Meligy

unread,
Oct 20, 2009, 10:54:41 AM10/20/09
to fluent-n...@googlegroups.com
So you think I should NOT create those as composite key parts and create them as non-key references? Please proide small code sample if I'm not getting it right.

Thanks a lot.


Regards,

--
Mohamed Meligy
Information Analyst (.Net Technologies) – Applications Delivery - TDG
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624

E-mail: eng.m...@gmail.com
Weblog: http://weblogs.asp.net/meligy


Mohamed Meligy

unread,
Oct 20, 2009, 10:59:59 AM10/20/09
to fluent-n...@googlegroups.com
Hey...
Sorry. There was a problem in the model I was talking about: I just copied the sample without modiifting.
The right model is:


> > CustomerOrders
> > --------------------
> > ID [Primary Key]
> > Name
>
> > OrderItems [Reference table]
> > ----------------------
> > OrderID [Compiste Key Part]
> > ProductID [Compiste Key Part]

>
> > Products
> > ---------------------
> > ID [Primary Key]
> > Name

Notice that now OrtderITems is JUST a n-n middle table, and that OrderID and ProductID are CompsiteId parts. IF this is the case, how do I cascade Order or Product in the mapping? Givin that CompositeID().KeyReference(....) does NOT seem to provide Cascade property. How can I achieve that still?



Regards,

--
Mohamed Meligy
Information Analyst (.Net Technologies) – Applications Delivery - TDG
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624

E-mail: eng.m...@gmail.com
Weblog: http://weblogs.asp.net/meligy


Hudson Akridge

unread,
Oct 20, 2009, 3:53:49 PM10/20/09
to fluent-n...@googlegroups.com
Ah, that changes the game slightly. If your orderlines is nothing more than a join table, then it's not really an entity. It would only be considered an entity (in NH lingo) if you had a third field you had a specific bit of information you wanted to track, such as the Quantity. If you have no quantity field (and I'd cautiously ask why in this case), then this is a simple Many-To-Many association between Order and Product. No trickery at all. Follow basic rules and examples for a manytomany
Reply all
Reply to author
Forward
0 new messages