nhibernate多对一父级在插入时始终为null [英] nhibernate many-to-one parent is always null on insert

查看:116
本文介绍了nhibernate多对一父级在插入时始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF中使用NHibernate 3.1.0.4000和AutoMapper 2.0.0.0.我有一个父母子女关系,我想从很多"方面开始维持.如果我从一个"端开始进行操作,则对维护对象没有任何问题,但是在这种情况下,这没有任何意义.我的问题是无论我如何更改映射,POCO等.尝试添加子项时,父对象中的父对象为null都会导致插入失败.我缺少让孩子中的父母财产填充的东西吗? 我在下表中定义了父子关系:

I am using NHibernate 3.1.0.4000 and AutoMapper 2.0.0.0 in a WCF. I have a parent-child relationship I want to maintain from the "many" end. I have no problems maintaining the objects if I do it from the "one" end but in this case that does not make sense. My issue is no matter how I change my mappings, POCOs, etc. the parent object when I attempt to add a child is null in the child causing the insert to fail. What am I missing to get the parent property in the child to populate? I have a parent-child relationship defined in the following tables:

    Create Table Attribute (AttributeUID uniqueidentifier, LongName varchar(20))
    Create Table AnswerOption (AnswerOptionID int, AttributeUID uniqueidentifier)

我希望属性(父级)成为所有者,因此我在该映射文件中而不是在AnswerOption(子级)中声明关系.虽然,我也尝试过双向关系,但这并没有改变测试中的任何行为.我的映射如下所示.属性:

I want the Attribute (parent) to be the owner so I declare the relationship in that mapping file and not in the AnswerOption (child). Though, I have tried with having the relationship bidirectional as well and that has not changed any behaviors in my tests. My mappings apear as follows. Attribute:

    <class name="RCAttribute" table="rcs.tblAttribute">
     <cache usage="read-write"/>
      <id name="ID">
        <column name="AttributeUID" />
        <generator class="guid" />
      </id>
      <property name="LongName" type="string" not-null="true" length="200" column="LongName" />
      <bag name="AnswerOptions" lazy="true" inverse="true" cascade="all">
        <key column="AttributeUID"/>
        <one-to-many class="AnswerOption" />
      </bag>
    </class>

AnswerOption:

AnswerOption:

    <class name="AnswerOption" table="rcs.tblAnswerOption" lazy="true">
      <cache usage="read-write"/>
      <id name="ID">
        <column name="AnswerOptionID" />
        <generator class="native" />
      </id>
    </class>

属性类:

    [Serializable]
    public class RCAttribute
    {
        public virtual Guid ID { get; set; }
        public virtual string LongName { get; set; }
        public virtual ICollection<AnswerOption> AnswerOptions { get; set; }

        public RCAttribute() { ID = new Guid("00000000-0000-0000-0000-000000000000"); }
     }

AnswerOption类别:

AnswerOption Class:

   [Serializable]
   public class AnswerOption
   {
       public virtual int ID { get; set; }

       public AnswerOption() { ID = 0; }
    }

我的测试过程如下:

    public void CreateAnswerOption()
    {
        AnswerOption newOpt = new AnswerOption();
        Attribute.AnswerOptions.Add(newOpt);
        Attribute = rc.RCAttributeSave(Attribute);
    }

创建此选项时,AnswerOption的Attribute属性为null,因此无法插入,因为在这种情况下,父级在子级中不能为null.我缺少什么来填充子项上的父级属性并能够插入?

When it goes to create this the Attribute property of the AnswerOption is null so it cannot insert since the parent cannot be null in the child in this case. What am I missing to get it to populate the parent property on the child and be able to insert?

推荐答案

将集合映射为逆集合而不在另一侧映射多对一是没有意义的.

Mapping the collection as inverse without mapping the many-to-one on the other side makes no sense.

您有三个选择:

  1. 在收集端使用inverse="true"进行双向映射,并在保存子级之前在代码中设置多对一属性(父级引用)(是的,我读到您不想这样做)方式).
  2. 仅映射收集侧(反).然后,NHibernate将首先插入带有NULL的子代作为父代引用,但是将在同一事务中使用正确的父代ID更新它.因此,您不能对子表中的父ID列施加非null约束(至少它必须是可延迟的).
  3. (此选项仅适用于NHibernate 3.2.0或更高版本)与选项2相同,但将not-null="true"添加到集合映射中的key标记中.然后,NHibernate将插入已经设置了父ID的孩子.
  1. Use a bidirectional mapping with inverse="true" on the collection side and set the many-to-one propery (parent reference) in your code before saving the child (yes, I read that you don't want to do it that way).
  2. Only map the collection side (not inverse). NHibernate will then first insert the child with NULL as parent reference, but will update it with the correct parent ID in the same transaction. So you can't have a not null constraint on the parent ID column in the child table (at least it must be deferrable).
  3. (This option only works with NHibernate 3.2.0 or newer) Same as option 2, but add not-null="true" to the key tag in the collection mapping. Then NHibernate will insert the child with the parent ID already set.

这篇关于nhibernate多对一父级在插入时始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆