Many to Many mapping problem - Child Collection always empty
I have the following situation. I have a Movie object which contains a list of characters, and the characters each have an person (actor). Additionally, I want the actor objects to contain a list of characters.
I've set up my mappings in fluent-nhibernate, and everything seems to be working perfectly, except that the Person.Characters collection is always empty. What's strange is the Characters.Person object is populated correctly, and the Movie.Characters collection is populated correctly. It's just the Person.Characters that always remains blank.
Here are the mappings I am using:
public class MovieMap : ClassMap<Movie>
{
public MovieMap()
{
Id(x => x.Id, "movie_id")
.GeneratedBy.Assigned();
Map(x => x.Title);
HasMany<Character>(x => x.Characters)
.KeyColumn("movie_id")
.Inverse()
.Cascade.All()
.Not.LazyLoad();
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id, "person_id")
.GeneratedBy.Assigned();
Map(x => x.Name);
HasMany<Character>(x => x.Characters)
.KeyColumn("person_id")
.Inverse()
.Cascade.All()
.Not.LazyLoad();
}
}
public class CharacterMap : ClassMap<Character>
{
public CharacterMap()
{
Id(x => x.Id)
.GeneratedBy.Native();
Map(x => x.Name);
References(x => x.Movie, "movie_id")
.ForeignKey("movie_id")
.Cascade.All();
References(x => x.Person, "person_id")
.ForeignKey("person_id")
.Cascade.All();
}
}
And here are my开发者_运维百科 classes:
public class Person
{
public Person(int id)
{
Id = id;
Characters = new List<Character>();
}
public virtual long Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Character> Characters { get; set; }
}
public class Movie
{
public Movie(int Id)
{
Id = id;
Characters = new List<Character>();
}
public virtual long Id { get; private set; }
public virtual string Title { get; set; }
public virtual IList<Character> Characters { get; set; }
}
public class Character
{
public Character()
{
}
public virtual int Id { get; private set; }
public virtual Movie Movie { get; set; }
public virtual Person Person { get; set; }
public virtual string Name { get; set; }
}
Have you tried retrieving an individual Actor or Character? Errors with these mappings can silently fail looking like 0 records where found.
精彩评论