多对多映射问题 - 子集合始终为空

发布于 2024-08-31 20:55:19 字数 2291 浏览 4 评论 0原文

我有以下情况。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2024-09-07 20:55:19

您是否尝试过检索单个演员或角色?这些映射的错误可能会默默地失败,看起来就像找到了 0 条记录。

Have you tried retrieving an individual Actor or Character? Errors with these mappings can silently fail looking like 0 records where found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文