-->
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.  [ 4 posts ] 
Author Message
 Post subject: Help! Persisting Collection
PostPosted: Mon Jun 04, 2007 1:23 pm 
Newbie

Joined: Tue Mar 28, 2006 2:52 pm
Posts: 5
Location: SJRP - SP - Brasil
Hello,
I have this code.

cliente.hbm.xml
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Expressa" namespace="Expressa.Model">
   <!--mapeamento para cadastro de pessoa física-->
  <class name="Cliente" table="clientes">
      <id name="Id" column="cliente_id" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
    <property column="razao_social" type="String" name="RazaoSocial" length="200" />
    <property column="nome" type="String" name="Nome" length="200" />
    <property column="email" type="String" name="Email" length="200" />
    <property column="cpf_cnpj" type="String" name="CpfCnpj" length="18" />
    <property column="rg" type="String" name="Rg" length="20" />
    <property column="tipo_pessoa" type="String" name="TipoPessoa" length="20" />
    <property column="inscricao_estadual" type="String" name="InscricaoEstadual" length="50" />
    <property column="data_nascimento" type="DateTime" name="DataNascimento" />
    <property column="telefone_residencial" type="String" name="TelefoneResidencial" length="20" />
    <property column="telefone_comercial" type="String" name="TelefoneComercial" length="20" />
    <property column="responsavel" type="String" name="Responsavel" length="100" />
    <property column="senha_criptografada" type="String" name="SenhaCriptografada" length="48" />
    <property column="sexo" type="String" name="Sexo" length="1" />
    <property column="newsletter" type="Boolean" name="Newsletter"/>
   
    <!--cliente pode possuir muitos endereços-->
    <bag name="_enderecos" access="field" table="cliente_enderecos" lazy="true" cascade="save-update">
      <key column="cliente_id" />
      <one-to-many class="Expressa.Model.ClienteEndereco, Expressa" />
    </bag>

  </class>
</hibernate-mapping>


ClienteEndreco.hbm.xml
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Expressa" namespace="Expressa.Model">
   <class name="ClienteEndereco" table="cliente_enderecos">

      <id name="Id" column="cliente_endereco_id" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
   
      <property column="apelido" type="String" name="Apelido" length="50" />
      <property column="rua" type="String" name="Rua" not-null="true" length="200" />
      <property column="numero" type="String" name="Numero" not-null="true" length="5" />
      <property column="complemento" type="String" name="Complemento" length="50" />
      <property column="bairro" type="String" name="Bairro" not-null="true" length="100" />
      <property column="cidade" type="String" name="Cidade" not-null="true" length="150" />
      <property column="estado" type="String" name="Estado" not-null="true" length="2" />
      <property column="cep" type="String" name="Cep" not-null="true" length="9" />
      <property column="referencia" type="String" name="Referencia" length="200" />
    <property column="cobranca" type="Boolean" name="Cobranca"  />
    <property column="entrega" type="Boolean" name="Entrega"  />

    <many-to-one name="Cliente" column="cliente_id" class="Expressa.Model.Cliente, Expressa" fetch="join" />
      
   </class>
</hibernate-mapping>


When I trying persist the collection "Enderecos" in "Cliente", the SQL generated:

Code:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;BEGIN TRANSACTION
go
exec sp_executesql N'INSERT INTO expressa.dbo.clientes (razao_social, nome, email, cpf_cnpj, rg, tipo_pessoa, inscricao_estadual, data_nascimento, telefone_residencial, telefone_comercial, responsavel, senha_criptografada, sexo, newsletter) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13); select SCOPE_IDENTITY()', N'@p0 nvarchar(1),@p1 nvarchar(12),@p2 nvarchar(16),@p3 nvarchar(11),@p4 nvarchar(4000),@p5 nvarchar(6),@p6 nvarchar(4000),@p7 datetime,@p8 nvarchar(11),@p9 nvarchar(4000),@p10 nvarchar(1),@p11 nvarchar(6),@p12 nvarchar(1),@p13 bit', @p0 = N'-', @p1 = N'Samir Mamude', @p2 = N'm', @p3 = N'000000000', @p4 = NULL, @p5 = N'física', @p6 = N'', @p7 = 'Dec  6 1979 12:00:00:000AM', @p8 = N'17 32356742', @p9 = N'', @p10 = N'-', @p11 = N'qqqqqq', @p12 = N'M', @p13 = 1
go
exec sp_executesql N'INSERT INTO expressa.dbo.cliente_enderecos (apelido, rua, numero, complemento, bairro, cidade, estado, cep, referencia, cobranca, entrega, cliente_id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11); select SCOPE_IDENTITY()', N'@p0 nvarchar(4000),@p1 nvarchar(38),@p2 nvarchar(4),@p3 nvarchar(4000),@p4 nvarchar(13),@p5 nvarchar(21),@p6 nvarchar(2),@p7 nvarchar(9),@p8 nvarchar(4000),@p9 bit,@p10 bit,@p11 int', @p0 = NULL, @p1 = N'2833/2834 ao fim', @p2 = N'4006', @p3 = NULL, @p4 = N't', @p5 = N'São José do Rio Preto', @p6 = N'SP', @p7 = N'120-030', @p8 = NULL, @p9 = 1, @p10 = 1, @p11 = NULL
go
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
go


But I got the error:
Code:
public void CreateCliente([DataBind("cliente")] Cliente cliente, [DataBind("endereco")] ClienteEndereco endereco)
        {
            cliente.RazaoSocial = "-";
            cliente.Responsavel = "-";

            CancelView();
            if (cliente.IsValid() == true && endereco.IsValid() == true)
            {                               
                //adiciona endereço
                cliente.Enderecos.Add(endereco);
                //salva o cadastro
                clienteService.Create(cliente);               
            }
            else
            {
                SendBackErrorMessage("Error");
                //envia mensagens de erro para o client
                ArrayList erros = new ArrayList();
                erros.Add(cliente.ErrorList);
                erros.Add(endereco.ErrorList);
                //envia respostar no formato JSON
                RenderText(ApplicationHelper.ErrorMessagesFor(erros));
            }
        }

ERROR Expressa.Model.Cliente - object references an unsaved transient instance - save the transient instance before flushing: Expressa.Model.ClienteEndereco


I don´t know what to do :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 5:13 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Oops - my bad. Didn't examine the problem well enough.

Symon.


Top
 Profile  
 
 Post subject: Solved!
PostPosted: Mon Jun 04, 2007 7:45 pm 
Newbie

Joined: Tue Mar 28, 2006 2:52 pm
Posts: 5
Location: SJRP - SP - Brasil
I remove the line, from the file ClienteEnderecos.hbm.xml and now its working.
Code:
<many-to-one name="Cliente" column="cliente_id" class="Expressa.Model.Cliente, Expressa" fetch="join" /


thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 4:15 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi mamude!

If you have bidirectional assocoation, you have to mark one of its side (<bag> in your mapping) as inverse="true".

_________________
WBR, Igor


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.