Good day all.
I'm with some difficulties with two related classes.
Code:
Empresa.hbm.xml
<class name="Entidades.Empresa, Entidades" table="tblEmpresa" lazy="true">
<!-- PK Composta -->
<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" />
.
.
.
<set name="Telefones" lazy="true" table="tblTelefone" cascade="delete">
<key>
<column name="telCodigoEmpresa"/>
<column name="telCodigoFilial"/>
</key>
<one-to-many class="Entidades.Telefone, Entidades" />
</set>
Let me traduce some names:
Empresa = Company
Telefone = Telephone
It's like a Company can have x telephones. One to Many association.
The problem is when i execute the command session.delete() it doesn't remove the related telephones from the database its only delete the company Ids and the registry stay there.
Code:
public static void Excluir(Empresa objEmpresa)
{
ISession session = NHibernateHelper.GetSession();
session.Delete(objEmpresa);
session.Flush();
session.Close();
}
Before delete command:Code:
ID Number CompanyId BranchId
1 32222-22222 1 1
After delete command:Code:
ID Number CompanyId BranchId
1 32222-22222 NULL NULL
Why i'm doing wrong?
I've tryed to split the method like this:
- Loop through phones and delete
- Delete the company
But i got the
"Illegally attempted to associate a proxy with two open Sessions." Error.
Can someone pls help me.