Diego Barreto
Diego Barreto

Reputation: 195

Add custom columns on many to many relationship on NHibernate

I have a movie entity and a actor entity, these 2 entities have a many to many relationship, so I've mapped it as ManyToMany(x=>x.Movies) and ManyToMany(x=>x.Actors) but i'd like to have the character the actor played on the movie, it should stay on the MoviesActorsPivot as a new column

But how can I do that using the Fluent Nhibernate Mapping in a way that I could get and save the data as easy as nhibernate does?

Not creating the pivot table manually and making the HasMany(x => x.MoviesActorsPivot) on both sides and managing the association by my own.

Edit:

Or if I map it creating the HasMany(x => x.MoviesActorsPivot) on both sides, how would i manage to insert and get al data like all movies from an actor or all actors that act on a movie, getting all the characters names?

Upvotes: 1

Views: 971

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

The answer is:

NHibernate native many-to-many mapping does not support any additional setting on the pairing table

But, it could be replaced with a pairing object being first level citizen

public class MovieActor 
{
    public virtual Movie Movie { get; set; }
    public virtual Actor Actor { get; set; }
    ... // more properties here
    public virtual int Rating { get; set; }
}

public class Actor
{
    public virtual IList<MovieActor> Movies { get; set; }
}

public class Movie 
{
    public virtual IList<MovieActor> Actors { get; set; }    
}

That would be standard HasMany and References mapping. And the queriyng later will be more easier

Also check these:

Upvotes: 1

Related Questions