-->
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.  [ 3 posts ] 
Author Message
 Post subject: How can I save composite-id objects ? (identifier altered ?)
PostPosted: Fri Dec 29, 2006 6:43 am 
Beginner
Beginner

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

Name and version of the database you are using: Ms SQL Server 2000

Hi, I cant use a composite-id object, nHibernate returns the following error. I am not altering any id of the object, just returning the objects from the DataBase.

Quote:
{"identifier of an instance of ControleMobiliario.DP.InscricaoMobiliaria altered from ControleCorporativo.ControleGeral.DP.InscricaoCadastralPK (ControleCorporativo.ControleGeral.DP.InscricaoCadastralPK) to ControleCorporativo.ControleGeral.DP.InscricaoCadastralPK (ControleCorporativo.ControleGeral.DP.InscricaoCadastralPK)" }


Mapping File
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="PMV_DP" namespace="ControleCorporativo.ControleGeral.DP">
   
   <class name="InscricaoCadastral" table="Geral_InscricaoCadastral" discriminator-value="0">
   
      <composite-id name="Id" class="InscricaoCadastralPK">
         <key-property name="Inscricao" column="inscricaoCadastral"/>
         <key-property name="TipoInscricao" column="tipoInscricao"/>
      </composite-id>
      <discriminator column="TipoInscricao" type="Int64" insert="false"/>
   </class>
</hibernate-mapping>


Class File
Code:
using System;
using Nullables;
using Utilitario.GerenciaDados;
using ControleCorporativo.ControleEndereco.DP;
using System.Collections;

namespace ControleCorporativo.ControleGeral.DP
{
   /// <summary>
   /// Summary description for InscricaoCadastral.
   /// </summary>
   public class InscricaoCadastral
   {
      #region Constructor
      public InscricaoCadastral()
      {         
      }
      #endregion

      #region Class Identifier
      private InscricaoCadastralPK id;
      public InscricaoCadastralPK Id
      {
         get { return this.id; }
         set { this.id = value; }
      }
      #endregion
     }
}


ID Class File
Code:
using System;

namespace ControleCorporativo.ControleGeral.DP
{
   /// <summary>
   /// Summary description for InscricaoCadastralPK.
   /// </summary>
   public class InscricaoCadastralPK
   {
      public InscricaoCadastralPK()
      {
      }

      #region identificadores da classe
      private long inscricao;
      public virtual long Inscricao
      {
         get { return this.inscricao;  }
         set { this.inscricao = value; }
      }
      
      private long tipoInscricao;
      public virtual long TipoInscricao
      {
         get { return this.tipoInscricao;}
         set { this.tipoInscricao = value;}
      }
      #endregion

      public override bool Equals(object obj)
      {
         InscricaoCadastralPK inscricaoCadastral = obj as InscricaoCadastralPK;

         if (inscricaoCadastral == null )
         {
            return false;
         }

         return base.Equals( (this.tipoInscricao == inscricaoCadastral.TipoInscricao) &&
                        (this.inscricao     == inscricaoCadastral.Inscricao) );
      }   

      public override int GetHashCode()
      {
         string strCode = this.tipoInscricao.ToString() + this.inscricao.ToString();
         return strCode.GetHashCode();
      }
   }
}



Class usage - How I am using it
Code:
StringBuilder strHql = new StringBuilder();
strHql.Append(" select distinct m from InscricaoMobiliaria m ");
strHql.Append(" inner join m.relAtividadesExercidas a ");
strHql.Append(" where getdate() >= a.Id.DataInicioAtividade and ");
strHql.Append(" m.Id.TipoInscricao = 1 and ");
strHql.Append(" ( a.dataTerminoAtividade is null OR getdate() <= a.dataTerminoAtividade ) ");
IList lstInscricoes = session.Find(strHql.ToString());



Can anyone tell me what I am doing wrong ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 1:28 pm 
Newbie

Joined: Mon Jan 08, 2007 11:55 am
Posts: 5
I think your classes need to be Serializable. Don't know if that solves the problem though...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 11:39 am 
Newbie

Joined: Fri May 18, 2007 1:41 pm
Posts: 8
This reply may be too late for the author, but in case anyone else has the problem...
When using composite IDs, make sure you override the object's Equals method correctly. That's what NHibernate is using to compare your objects to see if they've been altered or not.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.