NHibernate.StaleStateException: Recuento de filas inesperado: 0; esperado: 1
Hola chicos,
Estoy ocupado escribiendo un programa administrativo de back-end para un sistema que existe. He seleccionado NHibernate para mi solución de acceso a datos y soy bastante nuevo en él. Estoy experimentando el siguiente error en una relación padre / hijo:
NHibernate.StaleStateException: Recuento de filas inesperado: 0; esperado: 1
Este error se debe al hecho de que en mi código fuente agrego el nuevo objeto secundario a la colección secundaria de Administradores de reuniones de los padres. Cuando guardo el objeto padre, espero que también se agreguen los hijos, sin embargo, se genera un INSERT solo para el objeto padre. Nhibernate no genera un INSERT para el niño, sino que intenta ACTUALIZAR el niño a pesar de que no existe. Por lo tanto, aparece el mensaje de error que se muestra arriba. He buscado en todas partes en la web y documentación de nhibernate para este escenario, pero no he encontrado ninguna ayuda. La mayoría del código involucra claves externas que no son parte de la clave primaria, o las personas parecen estar lidiando con relaciones uno a uno o muchos a muchos. Necesito especificar el mapeo y el código para que al insertar el padre, los niños también se inserten. Por favor ayuda.
Mi estructura de datos es la siguiente:
Reunión - mesa principal
MeetingID (pk) (int, identidad)DescripciónFecha de inicioEstá activoLugar de encuentroMeetingAdministrator - tabla secundaria
MeetingID (pk, fk)AdminNetworkID (pk) (varchar)Fecha de creacionEstá activoY aquí está la fuente de 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
Aquí están los archivos de mapeo:
<!-- 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>