-->
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.  [ 2 posts ] 
Author Message
 Post subject: Strange one-to-many unidirectional behavior
PostPosted: Tue Apr 25, 2006 10:10 am 
Newbie

Joined: Tue Apr 25, 2006 9:36 am
Posts: 2
Hibernate is behaving very strangely with a 1:N unidirectional relationship between my classes Orcamento (1) and Trajeto (N). Table Trajeto has a foreign key to Orcamento. When I add a second trajeto to an orcamento, the foreign key of the previous trajetos is set to null, as the last line of the Hibernate SQL log below shows. Is this behavior the expected one?

Hibernate version: 3.1

Mapping documents:

Orcamento:
Code:
        <set
            name="trajetos"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

            <key
            >
                <column
                    name="orcamentoId"
                    not-null="true"
                />
            </key>

            <one-to-many
                  class="br.com.*****.Trajeto"
            />

        </set>


Code between sessionFactory.openSession() and session.close():
Code:
      Session currentSession = HibernateDAOFactory.getCurrentSession();
      Transaction tx = currentSession.beginTransaction();

      try {
         currentSession.saveOrUpdate(entity);
         tx.commit();
      }
      catch (HibernateException e) {
         e.printStackTrace();
         tx.rollback();
         throw e;
      }


Name and version of the database you are using:
MySQL 5.0.11

The generated SQL (show_sql=true):
Hibernate: insert into Orcamento (tempoDeViagem, ordemDeCompraGerada, quilometrosPorDia, custoAgregado, agregado, cgccpfCliente, condicoes, data, distancia, excessoDePeso, consumoDeOleo, quilometroPorTUV, observacao, operador, precoDepreciacao, precoDiarias, precoDiversos, precoEscolta, precoLicenca, precoOleo, precoPecas, precoPedagio, precoPneus, precoSeguro, quantidadeDepreciacao, quantidadeDiarias, quantidadeDiversos, quantidadeEscolta, quantidadeLicenca, quantidadeOleo, quantidadePecas, quantidadePedagio, quantidadePneus, quantidadeSeguro, seguro, ufir, comissao, cpmf, custoFinanceiro, icms, impostosFederais, investimento, iss, lucroReal, precoFinalDeVenda, multiplicadorIcms, responsabilidadesCliente, responsabilidadesContratado, numeroDeCaminhoes, custoAdministrativo, lastModificationTimestamp, centralId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Trajeto (indice, destino, origem) values (?, ?, ?)
Hibernate: insert into Trajeto (indice, destino, origem) values (?, ?, ?)
Hibernate: update Trajeto set orcamentoId=? where id=?
Hibernate: update Trajeto set orcamentoId=? where id=?
Hibernate: update Orcamento set tempoDeViagem=?, ordemDeCompraGerada=?, quilometrosPorDia=?, custoAgregado=?, agregado=?, cgccpfCliente=?, condicoes=?, data=?, distancia=?, excessoDePeso=?, consumoDeOleo=?, quilometroPorTUV=?, observacao=?, operador=?, precoDepreciacao=?, precoDiarias=?, precoDiversos=?, precoEscolta=?, precoLicenca=?, precoOleo=?, precoPecas=?, precoPedagio=?, precoPneus=?, precoSeguro=?, quantidadeDepreciacao=?, quantidadeDiarias=?, quantidadeDiversos=?, quantidadeEscolta=?, quantidadeLicenca=?, quantidadeOleo=?, quantidadePecas=?, quantidadePedagio=?, quantidadePneus=?, quantidadeSeguro=?, seguro=?, ufir=?, comissao=?, cpmf=?, custoFinanceiro=?, icms=?, impostosFederais=?, investimento=?, iss=?, lucroReal=?, precoFinalDeVenda=?, multiplicadorIcms=?, responsabilidadesCliente=?, responsabilidadesContratado=?, numeroDeCaminhoes=?, custoAdministrativo=?, lastModificationTimestamp=?, centralId=? where id=?
Hibernate: update Trajeto set indice=?, destino=?, origem=? where id=?
Hibernate: update Trajeto set indice=?, destino=?, origem=? where id=?
Hibernate: update Trajeto set orcamentoId=null where orcamentoId=? and id=?


Top
 Profile  
 
 Post subject: Ooops, wrong description
PostPosted: Tue Apr 25, 2006 10:31 am 
Newbie

Joined: Tue Apr 25, 2006 9:36 am
Posts: 2
That Hibernate SQL log, actually, is the one generated when I create an Orcamento, add two Trajetos to it, insert the Orcamento into the database (until here everything works properly) and then try to remove one of the added Trajetos.

On the other hand, the scenario I described above still happens: when I add one element to a collection (1:N mapping), the previous elements have their foreign keys set to null.


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

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.