NHibernate不将父级插入数据库 [英] NHibernate not inserting parent into the db

查看:80
本文介绍了NHibernate不将父级插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存一个新报表时,NHibernate插入该报表,忽略发布,并尝试插入UserPublication.但是,SQL随后抱怨违反FK约束. 就像NHibernate一样,即使该行在数据库中不存在也不认为Publication是新的.

When I save a new Report, NHibernate inserts the Report, ignores the Publication and tries to insert the UserPublication. However SQL then complains about violation of FK constraint. Its like NHibernate doesn't think the Publication is new even though the row doesn't exist in the db.

将实体关系视为: 报告可以有许多出版物(出版物属于报告) 一个出版物可以有许多UserPublication(UserPublications属于一个出版物)

Think of the entity relationship as: A Report can have many Publications (Publications belong to a Report) A Publication can have many UserPublications (UserPublications belong to a Publication)

有什么想法我做错了吗? 预先感谢.

Any ideas what I've done wrong? Thanks in advance.

以下是映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Model.Report, Model" table="Report" lazy="true">
  <id name="Id" access="property" column="ReportID">
    <generator class="assigned"></generator>
  </id>    
  <property name="DeleteUnread" access="property" />
  <property name="Description" access="property" />
  <property name="Name" access="property" />    
  <bag name="Publications" access="property" lazy="true" cascade="all-delete-orphan">
    <key column="ReportID"/>
    <one-to-many class="Model.Publication, Model"/>        
  </bag>
</class>  
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="Model.Publication, Model" table="Publication" lazy="true">
    <id name="Id" access="property" column="PublicationID">    
      <generator class="assigned"></generator>
    </id>  
    <property name="CreatedOn" access="property" />
    <property name="FileExtension" access="property" /> 
    <property name="IsDownloaded" access="property" />
    <property name="ToBeDownloaded" access="property" />
    <property name="Name" access="property"/>  
    <bag name="UserPublications" access="property" lazy="true" cascade="all-delete-orphan">    
      <key column="PublicationID"></key>
      <one-to-many class="Model.UserPublication, Model" />
    </bag>
    <many-to-one name="Report" class="Model.Report, Model" lazy="false" column="ReportID" not-null="true" cascade="none">
    </many-to-one>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="Model.UserPublication, Model" table="UserPublication" lazy="true">
    <id name="Id" access="property" column="UserPublicationID">
      <generator class="native"></generator>
    </id>
    <property name="IsFlaggedForDeletion" access="property" column="IsFlaggedForDeletion" />
    <property name="HasBeenRead" access="property" column="HasBeenRead" />
    <property name="DateReceived" access="property" column="DateReceived" />
    <property name="MustRead" access="property" column="MustRead" />
    <property name="ShowToolbar" access="property" column="ShowToolbar" />
    <property name="MaxAge" access="property" column="MaxAge" />
    <property name="FeedId" access="property" column="FeedId" />
    <property name="CanEdit" access="property" column="CanEdit" />    
    <many-to-one name="User" access="property" column="ClientUserID" class="Model.ClientUser, Model" not-null="true" cascade="none">      
    </many-to-one>
    <many-to-one name="Publication" access="property" class="Model.Publication, Model" column="PublicationID" not-null="true" cascade="none">      
    </many-to-one>
</class>

推荐答案

这有效.我将unsaved-value属性设置为"any".

This works. I set the unsaved-value attribute to "any".

我认为不会有任何回弹.

I don't think there will be any repurcussions.

<id name="Id" access="property" column="PublicationID" unsaved-value="any">    
  <generator class="assigned"></generator>
</id>

这篇关于NHibernate不将父级插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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