Bi-directional NHibernate mapping by code

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:41:31

问题


Just to be clear - this question is not about Fluent NHibernate.

I have a Parent and Child classes, with a one-to-many relationship between them.

The code is shortened for readability.

public class Child
{
    int Id;
    string Name;
}

public class Parent
{
    int Id;
    string Name;
    Iesi.Collections.Generic.ISet<Child> Children;
}

public ChildMapping()
{
    Table("Children");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });
}

public ParentMapping()
{
    Table("Parents");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    Set(p => p.Children, m => {
        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Key(k => {
            k.Column("ParentId");
            k.NotNullable(true);
        });
    }, a => a.OneToMany());
}

The Child class needs a Parent property on it. The Parent needs to control the relationship (I can't set the Inverse to true on the Parent's end).

How should the Parent and Child mapping look like?


回答1:


I've added the following items:

Child class: a field _parent, a constructor
Parent class: AddChild method
Child mapping: manytoone for parent property
Parent mapping: inverse(true) to make parent handler of the children

Complete code:

public class Child
{
    int Id;
    string Name;
    Parent _parent;

    public Child(Parent parent)
   {
     _parent = parent;
   }
}

public class Parent
{
    int Id;
    string Name;
    Iesi.Collections.Generic.ISet<Child> Children;

    public virtual Child AddChild()
    {
       Child newChild = new Child(this); //link parent to child via constructor
       Children.Add(newChild); //add child to parent's collection
       return newChild; //return child for direct usage
    }
}

public ChildMapping()
{
    Table("Children");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    ManyToOne(x => x.Parent, map => {
         map.Column("Id"); /* id of the parent table */
         map.Access(Accessor.Field);
         map.NotNullable(true);
         map.Class(typeof(Parent));
        });    
}

public ParentMapping()
{
    Table("Parents");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    Set(p => p.Children, m => {
        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Key(k => {
            k.Column("Id"); /* id of the child table */
            k.NotNullable(true);
            k.Inverse(true); /* makes the parent handle it's childlist */
        });
    }, a => a.OneToMany());
}

that should work.



来源:https://stackoverflow.com/questions/12095274/bi-directional-nhibernate-mapping-by-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!