NHibernate可选联接生成插入而不是更新 [英] NHibernate Optional Join generates insert instead of update

查看:139
本文介绍了NHibernate可选联接生成插入而不是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用NHibernate来支持一个灵活的实体模型。系统字段存储在一个表中,动态字段存储在扩展表中。这些表共享相同的主键。

我们使用JOIN组件来映射扩展表中的字典。 JOIN组件被映射为可选项,但会发生以下错误:


  1. 创建一个没有动态属性的新实体。 Hibernate在第二个表中存储一个空记录 - 除了foreign / primary键之外,所有的列都被存储为null。加载创建的记录并设置一个动态字段。

  2. 保存更新后的记录会导致错误。 NHibernate将发出一个新的插入,而不是一个更新语句。


    目前在Nhibernate中有一个错误可能是相关的为此:
    https://nhibernate.jira.com/browse/NH-2876


    我想知道是否有解决这个NHibernate的核心错误,或者如果有人有一个解决方法吗?



    谢谢!

    解决方案

    通过使用映射到一个指向组件上的id的函数来工作:

    $ p $ < hibernate-mapping
    xmlns =urn:nhibernate-mapping-2.2
    assembly =NHibernate.Test
    namespace =NHibernate.Test.NHSpecificTest.NH2876
    default-lazy =false>

    < class name =Customer>
    < id name =Id>
    < generator class =guid/>
    < / id>
    < property name =Namecolumn =Name/>
    < join table =CustomerDataoptional =true>
    < key column =CustomerIdunique =true/>
    < component name =ExtendedData>
    < property name =CustomerIdformula =[CustomerId]insert =falseupdate =false/>
    < property name =SomeData/>
    < / component>
    < / join>
    < / class>





    这也要求CustomerId属性被添加到组件类。

    发生这种情况的原因是,当NHibernate拉取现有记录时,如果没有非相关组件,空值。所以当它在AbstractEntityPersister中调用UpdateOrInsert时检查旧值时,它只会看到组件属性的空对象,因此认为它是一个插入。当表中没有记录时,这是相同的行为。通过使用键字段添加映射的公式字段,组件属性在旧字段中具有值,并且它正确地知道它需要执行更新。



    这里是一个额外的单元测试和工作解决方案(与NHibernate.Test项目一起使用)的回购,基于这里找到的原始单元测试 - https://nhibernate.jira.com/browse/NH-2876



    https://github.com/kfehribach/NH2876

    Kent

    p>

    We are using currently NHibernate to support a flexible entity model. The system fields are stored in one table and the dynamic fields are stored in an extension table. The tables shared the same primary keys.

    We use a JOIN component to map a dictionary in the extension table. The JOIN component is mapped as optional but the following bug occurss:

    1. Create a new entity with no dynamic attributes. Hibernate stores an empty record in the second table - all columns are stored as null except for the foreign/primary key.

    2. Load the created record and set a dynamic field.

    3. Save the updated record will result in an error. NHibernate will issue a new insert instead of an update statement.

    There is currently a bug in Nhibernate which may be related to this: https://nhibernate.jira.com/browse/NH-2876.

    I'm wondering if currently there is a solution to this NHibernate Core bug or if anyone has a workaround to it?

    Thank you!

    解决方案

    I was able to get the code to work by using mapping to a function that points to the id on the component:

    <hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
    assembly="NHibernate.Test"
    namespace="NHibernate.Test.NHSpecificTest.NH2876"
    default-lazy="false">
    
    <class name="Customer">
        <id name="Id">
            <generator class="guid" />
        </id>
        <property name="Name" column="Name" />
        <join table="CustomerData" optional="true">
            <key column="CustomerId" unique="true" />
            <component name="ExtendedData">
                <property name="CustomerId" formula="[CustomerId]" insert="false" update="false" />
                <property name="SomeData" />
            </component>
        </join>
    </class>
    

    This also requires that the CustomerId property be added to the component class.

    The reason that this happens is that when NHibernate pulls the existing record it does not populate the the related component if there are no non-null values. So when it checks the "old values" when calling UpdateOrInsert in AbstractEntityPersister it sees just a null object for the component attribute and thus thinks it is an insert. This is the same behavior when there is no record in the table. By adding the mapped formula field using the key field the component attribute has a value in the "old fields" and it properly knows that it needs to perform an update.

    Here is a repo with the additional unit tests and the working solution (to be used with the NHibernate.Test project) based on the original unit tests found here - https://nhibernate.jira.com/browse/NH-2876:

    https://github.com/kfehribach/NH2876

    Kent

    这篇关于NHibernate可选联接生成插入而不是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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