Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: Many-to-many relationship with field in the relationship table

I'm scratching my head; I have a Car table and a Customer table that have a many-to-many relationship. In this relationship table I want to add a column that can tell me what kind of relationship this is; is the customer testdriving the car, do he want to buy the car, ect. What I want to end up with is a class Car object that holds a collection of Customers and the relationship information. I might be looking at this the wrong way so feel free to push me in the right direction.

like image 277
Fossmo Avatar asked Apr 26 '10 12:04

Fossmo


2 Answers

Make the relationship an entity:

class CarRelation
{
  Car Car {get; set; }
  RelationType Type {get; set;}
}

A pure many-to-many relation doesn't have any additional properties.

like image 142
Stefan Steinegger Avatar answered Nov 15 '22 09:11

Stefan Steinegger


I think that you are missing an additional entity. You need to add an entity that expresses the customer's interest in the car. You will need to find the right name to fit your business domain, but here is my guess:

You have CUSTOMER table to store information about a specific customer. CUSTOMER has a one to many relationship to the CUSTOMERINTEREST table. CUSTOMERINTEREST stores information about the kinds of shopping activities the customer is engaged in (test drives, window shopping, etc.). CUSTOMERINTEREST has a many to one relationship with the CAR table. CAR stores information about specific automobiles in the store's inventory.

So, my best guess is that if the thing in the middle of your relationship needs more columns than just the two foreign keys then you don't really have a many to many relationship. You have two many to one relationships and you just haven't identified the thing in the middle yet.

like image 23
Mark Ewer Avatar answered Nov 15 '22 11:11

Mark Ewer