-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite identifiers, need help.
PostPosted: Wed Apr 30, 2008 3:33 pm 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
I have a class with composite identifier and im having some difficulties, if anyone could help me i'd appreciate.
Class Empresa:
Code:
public class Empresa
    {
        public Empresa()
        { }

        private EmpresaPK _empresaPK;
        private string _razaoSocial;
        private string _cgc;

        public virtual EmpresaPK EmpresaPK
        {
            get { return _empresaPK; }
            set { _empresaPK = value; }
        }

        public virtual string RazaoSocial
        {
            get { return _razaoSocial; }
            set { _razaoSocial = value; }
        }

        public virtual string CGC
        {
            get { return _cgc; }
            set { _cgc = value; }
        }
   }


Composite Id

Code:
    [Serializable]
    public class EmpresaPK
    {
        public EmpresaPK()
        { }

        private int _codigo;
        private int _codigoFilial;

        public int Codigo
        {
            get { return _codigo; }
            set { _codigo = value; }
        }

        public int CodigoFilial
        {
            get { return _codigoFilial; }
            set { _codigoFilial = value; }
        }

        public override bool Equals(object obj)
        {

            if (obj == this) return true;
            if (obj == null) return false;

            EmpresaPK that = (EmpresaPK)obj;
            if (that == null)
            {
                return false;
            }
            else
            {
                if (this._codigo != that._codigo) return false;
                if (this._codigoFilial!= that._codigoFilial) return false;
                return true;
            }

        }

        public override int GetHashCode()
        {
            return Codigo.GetHashCode() ^ CodigoFilial.GetHashCode();
        }
    }


HBM.XML:
Code:
hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Entidades.Empresa, Entidades" table="tblEmpresa" lazy="true">

      <composite-id name="EmpresaPK" class="Entidades.EmpresaPK,Entidades">
         <key-property name="Codigo" column="empCodigo" type="Int32" />
         <key-property name="CodigoFilial" column="empCodigoFilial" type="Int32" />
      </composite-id>
      
      <property name="RazaoSocial" column="empRazaoSocial" type="String" length="60" />
      <property name="CGC" column="empCgc" type="String" length="18" />
   </class>
</hibernate-mapping>


I'm getting the following errors when i try to .Save the object:
"ids for this class must be manually assigned before calling save(): Entidades.Empresa"

Im doing this:
Code:
EmpresaPK empresaPK = new EmpresaPK();
empresaPK.Codigo = 1;
empresaPK.CodigoFilial = 1;

empresa.EmpresaPK = empresaPK;
empresa.RazaoSocial ="test";
empresa.CGC = "test";


Anyone knows why im getting this error?

Other question:
-I wanted to generate one of the fields myself but let the system generate the other field in the key. It's that possible?

_________________
Weekend PLEASE!!!!


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

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.