多对多映射问题 - 子集合始终为空
我有以下情况。我有一个 Movie 对象,其中包含一个角色列表,每个角色都有一个人(演员)。此外,我希望演员对象包含角色列表。
我已经在 Fluent-nhibernate 中设置了映射,除了 Person.Characters 集合始终为空之外,一切似乎都运行良好。奇怪的是,Characters.Person 对象已正确填充,Movie.Characters 集合也已正确填充。只是 Person.Characters 始终保持空白。
这是我正在使用的映射:
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();
}
}
这是我的类:
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; }
}
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过检索单个演员或角色?这些映射的错误可能会默默地失败,看起来就像找到了 0 条记录。
Have you tried retrieving an individual Actor or Character? Errors with these mappings can silently fail looking like 0 records where found.