Hi community,
I am working with nHibernate 1.0.3 and MsSqlSerser 2000.
I have a composite-id object but I can´t make it work properly.
With the class and mapping listed below I can query without problems.
But when I try to save an object, secont time, session is closed. I get the exception " the owned exception is closed".
And when I try to update theses objects, NHibernate does not detect that they are dirty and nothing happens. ( No update SQL is generated )
I already defined Equals and GetHashTable but I coudn´t implement the ISerializable interface since I couldn´t find an example of how do it anywhere.
I also tried to include version control in the object to prevent this problem bit no effect.
PS: I don´t if it is relevent but this class belongs to a inheritance.
CalculoPadraoISSQN, CalculoDiferenciadoISSQN inherit from CalculoISSQN but in order to simplify the problem they are not shown.
What steps am I missing or doing wrong to persist a composite-id object ?
Class File: CalculoISSQN.cs
Code:
using System;
using Nullables;
using ControleMobiliario.DP;
using ControleCorporativo.ControlePessoa.DP;
using ControleCorporativo.ControleGeral.DP;
using Utilitario.GerenciaDados;
using System.Runtime.Serialization;
namespace ControleAtividade.DP
{
/// <summary>
/// Summary description for CalculoISSQN.
/// </summary>
public class CalculoISSQN : ISerializable
{
int versao = 0;
#region Constructor
public CalculoISSQN()
{
}
#endregion
#region Class Identifiers
/// <summary> Numero que identifica o tipo de calculo ISSQN </summary>
protected long tipoCalculo;
public virtual long TipoCalculo
{
get { return this.tipoCalculo; }
set { this.tipoCalculo = value; }
}
/// <summary> Codigo do calculo ISSQN ( junto com o tipo de calculo é a chave ) </summary>
private long codCalculoISSQN;
public virtual long CodCalculoISSQN
{
get { return this.codCalculoISSQN; }
set { this.codCalculoISSQN = value; }
}
#endregion
public override bool Equals(object obj)
{
CalculoISSQN calculoISSQNObj = obj as CalculoISSQN;
if ( calculoISSQNObj == null ) return false;
if (( this.TipoCalculo == calculoISSQNObj.TipoCalculo )&&( this.CodCalculoISSQN == calculoISSQNObj.CodCalculoISSQN ))
{
return true;
}
return false;
}
public override int GetHashCode()
{
double unique_id = (long)this.tipoCalculo*(Math.Pow(2.0,16.0)) + this.CodCalculoISSQN;
return unique_id.GetHashCode();
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(this.GetType());
}
#endregion
}
}
Mapping File: CalculoISSQN.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="SIAR_DP" namespace="ControleAtividade.DP">
<class name="CalculoISSQN" table="CNAE_CalculoISSQN">
<composite-id>
<key-property name="CodCalculoISSQN" column="codCalculoISSQN" />
<key-property name="TipoCalculo" column="tipoCalculo" />
</composite-id>
<version column="versao" name="versao" access="field" unsaved-value="0" />
</class>
</hibernate-mapping>