Hi there!
Currently i am developing a small wpf application using nhibernate . I am having a problem related to the session management using nhibernate. My mapping file is the follow :
Company.hbm.xml :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Save.Domain.Company, Save" table="Companies" >
<id name="Id" column="Id_Company" unsaved-value="-1" >
<generator class="native" />
</id>
<property name="Name" not-null="true" column="CmpName"/>
<property name="ShortName" not-null="true" column="ShName"/>
<property name="NIPC" not-null="false" column="NIPC"/>
<property name="State" type="Save.Domain.State, Save" not-null="true"/>
<property name="Type" type="Save.Domain.CompanyType, Save" not-null="true"/>
<many-to-one name="Business" class="Save.Domain.BusinessField, Save" not-null="true" column="Id_BusinessField"/>
<many-to-one name="ParentCompany" class="Save.Domain.Company, Save" column="Id_Parent_Company" not-null="false"/>
<bag name="Companies" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="Id_Parent_Company"/>
<one-to-many class="Save.Domain.Company, Save"/>
</bag>
<bag name="BpCompanies" cascade="save-update" inverse="true" lazy="false">
<key column="Id_Company"/>
<one-to-many class="Save.Domain.BPCompany, Save"/>
</bag>
<bag name="Employees" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="Id_Company"/>
<one-to-many class="Save.Domain.EmployeeCard, Save"/>
</bag>
</class>
</hibernate-mapping>
CompanyList.xaml ( Form )
Code:
public void DeleteCompany()
{
var record = listCompanies.ActiveRecord as DataRecord;
if(record!=null)
{
var company = record.DataItem as Domain.Company;
var session = NHibernateHelper.OpenSession();
if (!company.NotAssociatedWithBp)
{
MessageBox.Show("Erro : A empresa seleccionada encontra-se associada a um ou mais planos de negócio. Tem de desactivar a empresa manualmente .",
"Eliminar Empresa");
return;
}
if (company.HasAssociatedCompanies)
{
MessageBox.Show(
"Erro : Não é possivel eliminar a empresa seleccionada. Existem outras unidades de negócio associadas. ",
"Eliminar Empresa");
return;
}
if(company.HasEmployees)
{
MessageBox.Show("Erro : Existem empregados afectos á empresa seleccionada.", "Eliminar Empresa");
return;
}
try
{
var res = MessageBox.Show("Tem a certeza que pretende eliminar a empresa seleccionada ?",
"Eliminar Empresa", MessageBoxButton.YesNo);
if (res == MessageBoxResult.Yes)
{
dao.Remove(company);
listCompanies.DataSource = dao.GetByCompanyType(CompanyType.GRUPO, false, null);
}
}
catch (Exception)
{
MessageBox.Show("Ocorreu um erro ao eliminar uma empresa.", "Eliminar Empresa");
return;
}
session.Close();
}
else
{
MessageBox.Show("Seleccione a empresa que pretende eliminar.", "Eliminar Empresa");
}
}
In this method when a click a button, a company should be deleted if not associated with other entities . To see if there is an association with other entities, lazy load is used . The problem here is that I get an LazyInitializationException saying that the session isn't open where I previously opened one :(
Any hints ?
Best Regards,
Ivan Frias