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

I scratch my head; I have a Car table and a Client table, which has a many-to-many relationship. In this relationship table, I want to add a column that can tell me what kind of relationship it is; a customer buys a car, he wants to buy a car, etc. What I want in the end is the Car class, which contains a collection of Clients and relationship information. I might be looking at it wrong, so feel free to go in the right direction.

+6
nhibernate many-to-many
source share
2 answers

Make the relation an entity:

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

The pure many-to-many relationship has no additional properties.

+4
source share

I think you are missing an extra object. You need to add a company that expresses customer interest in the car. You will need to find the correct name to match your business domain, but here's my guess:

You have a CUSTOMER table to store information about a specific client. CUSTOMER has one to many relationship with the CUSTOMERINTEREST table. CUSTOMERINTEREST stores information about the types of trading operations the client is involved in (test drives, window purchases, etc.). CUSTOMERINTEREST has much to do with the CAR table. CAR stores information about specific cars in the store’s inventory.

So, I think that if in the middle of your relationship you need more columns than just two foreign keys, then you really don't have many relationships. You have two different relationships, and you have not yet defined the thing in the middle.

+4
source share

All Articles