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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Problems with Bags
PostPosted: Mon May 26, 2008 2:32 pm 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
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.

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 1:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You need cascade="all-delete-orphan" to get all children removed automatically.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: didn't work
PostPosted: Tue May 27, 2008 7:38 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
wolli wrote:
You need cascade="all-delete-orphan" to get all children removed automatically.


Man it didn't worked. I changed to cascade="all-delete-orphan" but the phones registry continues there with the company keys null. Do you need any other information to help me solve this?

Thanks

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 7:42 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the mapping of Telephones ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:06 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
Code:
<class name="Entidades.Telefone,Entidades" table="tblTelefone" lazy="true">
         
      <id name="Codigo" column="telCodigo" type="Int32">
         <generator class="increment"/>
      </id>

      <!-- Colunas   -->
      <property name="Origem" column="telOrigem" type="String" not-null="true" length="1" />
      <property name="Tip" column="telTipTelefone" type="String" length="1" />

        <!-- Relacionamentos -->
      <many-to-one name="Sindicato" column="telCodigoSindicato" fetch="select" cascade="none" />
      
      <!--//Chave Composta// -->
      <many-to-one name="Empresa">
         <column name="telCodigoEmpresa"/>
         <column name="telCodigoFilial"/>
      </many-to-one>


      <many-to-one name="Agencia">
         <column name="telCodigoBanco"/>
         <column name="telCodigoAgencia"/>
      </many-to-one>

</class>

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You've defined the association on both entities, so you have to mark one end as inverse:

Code:
<set name="Telefones" lazy="true" table="tblTelefone" cascade="all-delete-orphan" inverse="true">
</set>


But maybe the other associations on Telephones will also cause problems and prevent the objects from beeing deleted. If the hint above does not solve the problem try to comment the other associations and try again.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:25 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
If i comment the association on Phone.xml do i have to remove the "inverse=true" ??

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Yes, but keep this association, just comment the others.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:33 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
I tryed the "inverse=true" dind't work it showed a error message telling that cant delete company record.

So i on the phone.xml i commented the association with company.
And didn't worked too.. still leaving the rows on database with NULL on company id.

Company:
Code:
<set name="Telefones" lazy="true" table="tblTelefone" cascade="all-delete-orphan">
         <key>
            <column name="telCodigoEmpresa"/>
            <column name="telCodigoFilial"/>
         </key>
         <one-to-many class="Entidades.Telefone, Entidades" />      
</set>


Phone:
Code:
   <!--
      <many-to-one name="Empresa">
         <column name="telCodigoEmpresa"/>
         <column name="telCodigoFilial"/>
      </many-to-one>
-->


Man if u have msn or skype i cand send u the files if you prefer, i would appreciate a lot.

thanks for the help till now.

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Keep the Company-Phone association on both entities and mark the one on Company as inverse. But comment these

Code:
        <!-- Relacionamentos -->
      <many-to-one name="Sindicato" column="telCodigoSindicato" fetch="select" cascade="none" />
     
      <many-to-one name="Agencia">
         <column name="telCodigoBanco"/>
         <column name="telCodigoAgencia"/>
      </many-to-one>


on Phone. Are there any other associations on Company ? Enable sql logging and have a look at the generated statements.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 8:50 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
the company xml:
Code:
<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>
      
      <!-- Colunas Normais -->
      <property name="RazaoSocial" column="empRazaoSocial" type="String" length="60" />
      <property name="CGC" column="empCgc" type="String" length="18" />
      <property name="InscricaoEstadual" column="empInscricaoEstadual" type="String" length="15" />
      <property name="InscricaoMunicipal" column="empInscricaoMunicipal" type="String" length="15" />
      <property name="CodigoCEI" column="empCodigoCei" type="String" length="12" />
      <property name="Contato" column="empContato" type="String" length="20" />
      <property name="ContatoCpf" column="empContatoCpf" type="String" length="14" />
      <property name="Email" column="empEmail" type="String" length="40" />
      <property name="NumeroEndereco" column="empNumeroEndereco" type="Int32" />
      <property name="Complemento" column="empComplemento" type="String" length="20" />
      <property name="NumeroProprietario" column="empNumeroProprietario" type="Int32" />
      <property name="NumeroFamilia" column="empNumeroFamilia" type="Int32" />
      <property name="Simples" column="empSimples" type="String" />
      <property name="PorteEmpresa" column="empPorteEmpresa" type="String" length="2" />
      <property name="Cefip" column="empSefip" type="String" length="2" />
      <property name="Categoria" column="empCategoria" type="String" length="2" />
      <property name="CodigoFgts" column="empCodigoFgts" type="Int32" />
      <property name="InicioAtividade" column="empInicioAtividades" type="DateTime" />
      <property name="FimAtividade" column="empFimAtividades" type="DateTime" />
      <property name="Pat" column="empPat" type="Char" />
      <property name="ValorPatrimonio" column="empValorPatrimonio" type="Double" />
      <property name="Fantasia" column="empFantasia" type="String" length="50" />
      <property name="Matriz" column="empMatriz" type="Char" />
      <property name="PercentagemAdiantamento" column="empPercentagemAdiantamento" type="Double" />
      <property name="Arredondamento" column="empArredondamento" type="Double" />
      <property name="TipoRetencao" column="empTipoRetencao" type="Char" />
      <property name="MesaNoProcessamento" column="empMesanoProcessamento" type="String" length="7" />
      <property name="Pagamento13PrimeiraParcela" column="empPagamento13PrimeiraParcela" type="DateTime" />
      <property name="Pagamento13SegundaParcela" column="empPagamento13SegundaParcela" type="DateTime" />
      <property name="Pagamento13Complementar" column="empPagamento13Complementar" type="DateTime" />
      <property name="Conta" column="empConta" type="String" length="20" />
      <property name="Convenio" column="empConvenio" type="String" length="20" />
            
         
      <!-- FKs -->
      <many-to-one name="NaturezaJuridica" column="empCodigoNaturezaJuridica" not-null="true" fetch="select" cascade="none" />
      <many-to-one name="RamoAtividade" column="empCodigoRamoAtividade" not-null="true" fetch="select" cascade="none" />
      <many-to-one name="Sindicato" column="empCodigoSindicato" not-null="true" fetch="select" cascade="none" />
      <many-to-one name="Inss" column="empCodigoInss" not-null="true" fetch="select" cascade="none" />
      <many-to-one name="Cep" column="empCep" not-null="true" fetch="select" cascade="none" />
      
   
      <many-to-one name="Agencia">
         <column name="empCodigoBanco"/>
         <column name="empCodigoAgencia"/>
      </many-to-one>

      <set name="Telefones" lazy="true" table="tblTelefone" cascade="all-delete-orphan" inverse="true">
         <key>
            <column name="telCodigoEmpresa"/>
            <column name="telCodigoFilial"/>
         </key>
         <one-to-many class="Entidades.Telefone, Entidades" />      
      </set>
            
      
   </class>

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 9:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
So when you use this mapping for phones, it's still not working ? What exact error do you get and what statements does hibernate generate ?

Code:
<class name="Entidades.Telefone,Entidades" table="tblTelefone" lazy="true">
         
      <id name="Codigo" column="telCodigo" type="Int32">
         <generator class="increment"/>
      </id>
     
      <!--//Chave Composta// -->
      <many-to-one name="Empresa">
         <column name="telCodigoEmpresa"/>
         <column name="telCodigoFilial"/>
      </many-to-one>

</class>


Try to set all associations to lazy="false" to see if that helps.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 9:01 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
wolli wrote:
Enable sql logging and have a look at the generated statements.


How to do that? **shame**

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 9:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Put hibernate.show_sql="true" in your hibernate configuration and enable DEBUG logging.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 9:16 am 
Newbie

Joined: Wed Apr 16, 2008 1:20 pm
Posts: 16
Message:
"could not delete: [Entidades.Empresa#Entidades.EmpresaPK]
[SQL: DELETE FROM tblEmpresa WHERE empCodigo = ? AND empCodigoFilial = ?]"

_________________
Weekend PLEASE!!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.