NHibernate 未将父级插入数据库

发布于 2024-08-05 00:21:25 字数 3335 浏览 2 评论 0原文

当我保存新报告时,NHibernate 会插入该报告,忽略发布并尝试插入 UserPublication。然而 SQL 随后抱怨违反了 FK 约束。 就像 NHibernate 不认为 Publication 是新的,即使数据库中不存在该行。

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

您知道我做错了什么吗? 提前致谢。

这是映射:

<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>

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.

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.

Here's the mappings:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-08-12 00:21:26

我认为问题在于出版物的 id 是分配的 id,因此 NHibernate 无法识别何时应插入出版物。
当您刷新会话时,它首先插入所有插入的对象,然后更新所有更新的对象,然后删除所有已删除的对象。
所以我认为这会发生在这里:
您保存的报告包含包含 userpublications 的出版物。由于分配了出版物 id,NHibernate 假定它必须更新并忽略它,但 UserPublication id 是本机的,NHibernates 知道何时应该插入它并尝试插入它,因此会发生 FK 冲突。
要解决此问题,您可以向发布添加版本属性,以便 NHibernate 可以根据其版本值插入它。

I think the problem is that id of publications is an assigned id therefore NHibernate can't recognize when it should insert a publication.
When you flush a session it first insert all inserted objects , then it updates all updated objects and then deletes all deleted objects.
So I think that's going to happen here:
You save a Report that has publications that have userpublications.Since publication id is assigned NHibernate assumes it has to be updated and ignore it but UserPublication id is native and NHibernates knows when it should be inserted and tries to insert it thus a FK violation happens.
To solve this problem you can add a version property to publication so NHibernate can insert it based on its version value.

千鲤 2024-08-12 00:21:26

Publication 类中的 UserPublications 包具有错误的键元素。应该是:

<key column="PublicationID"/>

The UserPublications bag in the Publication class has a wrong key element. It should be:

<key column="PublicationID"/>
万人眼中万个我 2024-08-12 00:21:26

这有效。我将未保存值属性设置为“任意”。

我认为不会有任何报复。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文