简体   繁体   English

Nhibernate通过代码映射:多对多

[英]Nhibernate mapping by code: many to many

I have problems understanding proper mapping (mapping by code) for updating my data. 我在理解正确的映射(通过代码映射)以更新数据时遇到问题。 Here it is (simplified for the sake of clarity). 在这里(为清楚起见简化了)。

  • [Persons] table is just a dictionary and is immutable. [Persons]表只是一个字典,是不可变的。
  • [Objects] can be: [对象]可以是:
    • created, 创建,
    • modified with [persons]: 修改为[人员]:
      1. added to object or 添加到对象或
      2. removed from object or 从物体上移走或
      3. only modified metadata (status and dates). 仅修改的元数据(状态和日期)。

I can't figured it out how to properly map my classes. 我不知道如何正确映射我的班级。 No matter what kind of cascade I set I can't get nhibernate to: - NOT modify person records, - properly delete or modify records from objectperson table - it mostly just try to add new ones. 无论我设置哪种类型的层叠,我都无法使它休眠:-不修改个人记录,-从对象人表中正确删除或修改记录-它大多只是尝试添加新记录。 I suspect my mappings are completely wrong but cant figure it out. 我怀疑我的映射是完全错误的,但无法弄清楚。

I also tried to make composite key for objectperson table (makes more sense) but found out it is for some reason discouraged in nhibernate. 我还尝试为对象人表创建复合键(更合理),但由于某种原因,它在nhibernate中不建议使用。 That's why I added separate autoid column. 这就是为什么我添加了单独的autoid列。

I managed to make selecting records work (sample below) but SaveOrUpdate never works as expected... where I gone wrong? 我设法使选择记录起作用(下面的示例),但是SaveOrUpdate却无法按预期工作...我哪里出错了?

My classes: 我的课程:

t_object
{
    public virtual int ID { get; set; }
    public virtual string Number { get; set; }
    public virtual IEnumerable<t_objectperson> Persons { get; set; }
}
t_person
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual string Surename { get; set; }
    public virtual IEnumerable<t_objectperson> Objects { get; set; }
}
t_objectperson
{
    public virtual int ID { get; set; }
    public virtual t_object Object { get; set; }
    public virtual t_person Person { get; set; }
    public virtual int Status { get; set; }
    public virtual DateTime Added { get; set; }
    public virtual DateTime? Expired { get; set; }
}

My mappings: 我的映射:

    public t_object_map()
    {
        this.Table("rs_object");

        this.Id(c => c.ID, m =>
        {
            m.Column("Id");
            m.Generator(Generators.Identity);
        });
        this.Property(c => c.Number);
        Set(x => x.Persons 
            , c => {
                c.Key(k => k.Column("IdObject"));
                c.Cascade(Cascade.All);
            }
            , map => map.OneToMany(p => p.Class(typeof(t_objectperson)))                   
        );
    }

    public t_objectperson_map()
    {
        this.Table("rs_objectperson");
        this.Id(c => c.ID, m =>
        {
            m.Column("Id");
            m.Generator(Generators.Identity);
        });
        this.Property(c => c.ID);
        this.ManyToOne(p => p.Object, m =>
        {
            m.Column("IdObject");
            m.Cascade(Cascade.All);
            //m.NotNullable(true);
            m.Unique(false);
        });
        this.ManyToOne(p => p.Person, m =>
        {
            m.Column("IdPerson");
            //m.Cascade(Cascade.All);
            //m.NotNullable(true);
            m.Unique(false);
        });
    }
    public t_person_map()
    {
        this.Table("rs_person");
        this.Id(c => c.ID, m =>
        {
            m.Column("Id");
            m.Generator(Generators.Identity);
        });   
        this.Property(c => c.Name);
        this.Property(c => c.Surename);     
    }

How I get data: 我如何获取数据:

var query = session
                  .QueryOver<t_object>()
                  .Where(c => c.ID == ID)
                  .Fetch(c => c.Persons).Eager
                  .Fetch(x => x.Persons.First().Object).Eager
                  .OrderBy(c => c.Number).Asc
                  .List<t_object>();

Are you missing the collection mapping on t_person_map? 您是否错过了t_person_map上的集合映射?

You need to set Inverse(true) Set collection. 您需要设置Inverse(true)Set集合。

Here's a quick explanation of when to set inverse: 这是何时设置逆的简要说明:

one-to-many -> many-to-one 一对多->多对一

right side should be active (left with inverse="true"), to save on UPDATEs (unless left side is explicitly ordered) 右侧应处于活动状态(左侧带有inverse =“ true”),以保存UPDATE(除非明确指定左侧)

ie one side has to take control of relationship and you need to be consistent in the way you do this. 也就是说,一方必须控制关系,并且您需要保持一致的方式。

from: http://notherdev.blogspot.com/2012/04/nhibernates-inverse-what-does-it-really.html 来自: http : //notherdev.blogspot.com/2012/04/nhibernates-inverse-what-does-it-really.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM