NHibernate.StaleStateException:意外的行数:0;预期:1

时间:2010-09-18 10:57:34

标签: nhibernate nhibernate-mapping parent-child

Hl Guys,

我正忙着为存在的系统编写后端管理程序。我已经为我的数据访问解决方案选择了NHibernate,并且它是相当新的。我在父/子关系中遇到以下错误:

  

NHibernate.StaleStateException:意外的行数:0;预期:1

此错误是由于我的源代码中将新的子对象添加到父级的MeetingAdministrators子集合中。当我保存父对象时,我也希望添加子对象,但是只为父对象生成INSERT。 Nhibernate不为子节点生成INSERT,而是尝试更新子节点,即使它不存在。因此,它会显示上面显示的错误消息。我已经浏览了网络上的各个地方和这个场景的nhibernate文档,但没有找到任何帮助。大多数代码涉及不属于主键的外键,或者人们似乎正在处理一对一或多对多关系。我需要指定映射和代码,以便在插入父项时,子项也会被插入。请帮忙。

我的数据结构如下:

会议 - 父表

  • MeetingID(pk)(int,identity)
  • 说明
  • StartDate
  • IsActive
  • 地点

MeetingAdministrator - 子表

  • MeetingID(pk,fk)
  • AdminNetworkID(pk)(varchar)
  • dateCreated会
  • IsActive

这是Visual Basic .NET源代码:

<Serializable()> _
Public Class MeetingAdministrator

    Private _MeetingID As Integer
    Public Overridable Property MeetingID() As Integer
        Get
            Return _MeetingID
        End Get
        Set(ByVal value As Integer)
            _MeetingID = value
        End Set
    End Property

    Private _AdminNetworkID As String
    Public Overridable Property AdminNetworkID() As String
        Get
            Return _AdminNetworkID
        End Get
        Set(ByVal value As String)
            _AdminNetworkID = value
        End Set
    End Property

    Private _IsActive As Byte
    Public Overridable Property IsActive() As Byte
        Get
            Return _IsActive
        End Get
        Set(ByVal value As Byte)
            _IsActive = value
        End Set
    End Property

    Private _DateCreated As Date
    Public Overridable Property DateCreated() As Date
        Get
            Return _DateCreated
        End Get
        Set(ByVal value As Date)
            _DateCreated = value
        End Set
    End Property

    Private _LastModified As Date
    Public Overridable Property LastModified() As Date
        Get
            Return _LastModified
        End Get
        Set(ByVal value As Date)
            _LastModified = value
        End Set
    End Property

    Private _meeting As Meeting
    Public Overridable Property Meeting() As Meeting
        Get
            Return _meeting
        End Get
        Set(ByVal value As Meeting)
            _meeting = value
        End Set
    End Property

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Return MyBase.Equals(obj)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return MyBase.GetHashCode()
    End Function

End Class




Imports Iesi.Collections
Imports Iesi.Collections.Generic



Public Class Meeting

    Private _MeetingID As Integer
    Private _Description As String

    Public Overridable Property MeetingID() As Integer
        Get
            Return _MeetingID
        End Get
        Set(ByVal value As Integer)
            _MeetingID = value
        End Set
    End Property

    Public Overridable Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Private _StartDate As Date = Now
    Public Overridable Property StartDate() As Date
        Get
            Return _StartDate
        End Get
        Set(ByVal value As Date)
            _StartDate = value
        End Set
    End Property

    Private _IsActive As Byte
    Public Overridable Property IsActive() As Byte
        Get
            Return _IsActive
        End Get
        Set(ByVal value As Byte)
            _IsActive = value
        End Set
    End Property

    Private _DateCreated As Date
    Public Overridable Property DateCreated() As Date
        Get
            Return _DateCreated
        End Get
        Set(ByVal value As Date)
            _DateCreated = value
        End Set
    End Property

    Private _Venue As String
    Public Overridable Property Venue() As String
        Get
            Return _ Venue
        End Get
        Set(ByVal value As String)
            _ Venue = value
        End Set
    End Property

    Private _meetingAdministrator As ISet(Of MeetingAdministrator)
    Public Overridable Property MeetingAdministrators() As ISet(Of MeetingAdministrator)
        Get

            Return _meetingAdministrator
        End Get
        Set(ByVal value As ISet(Of MeetingAdministrator))
            _meetingAdministrator = value
        End Set
    End Property

    Public Overridable Sub AddAdministrator(ByVal meetingAdministrator As MeetingAdministrator)
        meetingAdministrator.Meeting = Me

        _meetingAdministrator.Add(meetingAdministrator)
    End Sub


    Public Sub New()
        _meetingAdministrator = New HashedSet(Of MeetingAdministrator)()

    End Sub
End Class

以下是映射文件:

<!-- Meeting.hbm.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data"
                    namespace="Data.Domain" >

  <!-- Mapping Information -->
  <class name="Meeting"  table="Meeting" >
    <id name="MeetingID" column="MeetingID" type="int">
      <generator class="identity" />
    </id>
    <property name="Description" />
    <property name="StartDate" />
    <property name="IsActive" />
    <property name="Venue" />
    <set name="MeetingAdministrators" table="MeetingAdministrator" inverse="true"  lazy="true"  cascade="save-update"  access="property" >
      <key column="MeetingID"  foreign-key="MeetingID"  />
      <one-to-many class="Meeting"  />
    </set>
  </class>
</hibernate-mapping>

<!-- MeetingAdministrator.hbm.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data"
                    namespace="Data.Domain" >

  <!-- Mapping Information -->
  <class name="MeetingAdministrator"  table="MeetingAdministrator" >
    <composite-id>
      <key-property  name="AdminNetworkID"  column="AdminNetworkID"  type="string"  >
      </key-property>
      <key-many-to-one name="Meeting" class="Meeting" >
        <column name="MeetingID" />
      </key-many-to-one>
    </composite-id>
    <property name="IsActive" />
    <property name="DateCreated" />
  </class>
</hibernate-mapping>

3 个答案:

答案 0 :(得分:8)

获得了相同的错误消息,但它是由删除触发的。原因很简单,条目已经被删除了。

答案 1 :(得分:6)

我很确定您需要在<version/>课程中添加MeetingAdministrator属性才能使其正常运行。有关进一步的讨论,请参阅this article

答案 2 :(得分:1)

对于使用AutoNumber / AutoIncrement for MySQL插入问题的任何人,请使用映射。我发现.Identity可能是气质的

Id(x => x.Id).GeneratedBy.Increment();