-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can´t make <composite-id> object work properly
PostPosted: Mon Nov 13, 2006 3:56 pm 
Beginner
Beginner

Joined: Wed Aug 09, 2006 10:15 am
Posts: 20
Location: Vitoria - ES - Brazil
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.xml

Code:
<?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>



Top
 Profile  
 
 Post subject: Example of a C# composite-id Class
PostPosted: Thu Nov 16, 2006 8:52 pm 
Beginner
Beginner

Joined: Wed Aug 09, 2006 10:15 am
Posts: 20
Location: Vitoria - ES - Brazil
Hi,

I guess all I need is a dummy C# composite-id class.
The most important thing is to understand how Equals, GetHashTable and specially how the Serializable interface was implemented.

I read several nHibernate docs and all I got was XML examples.

Can anybody post a sample code ??

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 10:58 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
I know of two examples.

http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/pks.html#CompositePK

http://www.hibernate.org/hib_docs/nhibernate/html/persistent-classes.html#persistent-classes-equalshashcode


Top
 Profile  
 
 Post subject: Final Solution
PostPosted: Fri Nov 17, 2006 7:55 am 
Beginner
Beginner

Joined: Wed Aug 09, 2006 10:15 am
Posts: 20
Location: Vitoria - ES - Brazil
Thanks very much K-Dub,

With your links and other articles I finally found the solution.
I am posting the solution for anybody else interested in learning.

CalculoISSQNPK.cs

Code:
   public class CalculoISSQNPK
   {
      #region 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 CalculoISSQNPK()
      {
      }

      public override bool Equals(object obj)
      {
         CalculoISSQNPK calculoISSQNObj = obj as CalculoISSQNPK;         
         if ( calculoISSQNObj == null ) return false;
         if (( this.TipoCalculo == calculoISSQNObj.TipoCalculo )&&( this.CodCalculoISSQN == calculoISSQNObj.CodCalculoISSQN ))
         {
            return true;
         }         
         return false;
      }   

      public override int GetHashCode()
      {
         return base.GetHashCode();
      }
   }


CalculoISSQN.cs

Code:
   public class CalculoISSQN
   {       
      #region Construtor
      public CalculoISSQN()
      {
         id = new CalculoISSQNPK();
      }
      #endregion

      #region Class Identifier ( using CalculoISSQNPK this time ! )
      protected CalculoISSQNPK id;
      public virtual CalculoISSQNPK Id
      {
         get { return this.id;  }
         set { this.id = value; }
      }
      #endregion

   }


CalculoISSQN.hbm.xml

Code:
<?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 name="Id" class="ControleAtividade.DP.CalculoISSQNPK">
         <key-property name="CodCalculoISSQN" column="codCalculoISSQN" />
         <key-property name="TipoCalculo" column="tipoCalculo" />                
      </composite-id>             
      
   </class>

</hibernate-mapping>
[/b]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.