NHibernate.StaleStateException: Contagem inesperada de linhas: 0; esperado: 1
Hl Guys,
Estou ocupado escrevendo um programa administrativo de back-end para um sistema que existe. Selecionei o NHibernate para minha solução de acesso a dados e sou bastante novo. Estou tendo o seguinte erro em um relacionamento pai / filho:
NHibernate.StaleStateException: Contagem inesperada de linhas: 0; esperado: 1
Esse erro é causado pelo fato de que, no meu código-fonte, adiciono o novo objeto filho à coleção de MeetingAdministrators dos filhos dos pais. Quando eu salvo o objeto pai, espero que os filhos também sejam adicionados, no entanto, um INSERT é gerado apenas para o objeto pai. O Nhibernate não gera um INSERT para o filho, mas tenta ATUALIZAR o filho, mesmo que ele não exista. Assim, aparece a mensagem de erro mostrada acima. Procurei em toda parte na web e não encontrei documentação para esse cenário, mas não encontrei nenhuma ajuda. A maioria dos códigos envolve chaves estrangeiras que não fazem parte da chave primária, ou as pessoas parecem estar lidando com relacionamentos um para um ou muitos para muitos. Preciso especificar o mapeamento e o código para que, na inserção do pai, os filhos sejam inseridos também. Por favor ajude.
Minha estrutura de dados é a seguinte:
Reunião - tabela pai
MeetingID (pk) (int, identidade)DescriçãoData de inícioEstá ativoLocalMeetingAdministrator - tabela filho
Identificação da reunião (pk, fk)AdminNetworkID (pk) (varchar)Data CriadaEstá ativoE aqui está a fonte do 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
Aqui estão os arquivos de mapeamento:
<!-- 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>