-->
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: Session management and Lazy Loading
PostPosted: Mon May 25, 2009 8:05 am 
Newbie

Joined: Fri Nov 14, 2008 7:43 am
Posts: 7
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


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.