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?