-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to save an association mapped with Set?
PostPosted: Fri Jul 27, 2007 12:41 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi.

I'm using NH 1.2.

I have an association (master/detail) mapped with the Set element. It's very simple:

Empresa.hbm.xml (master)

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dominio" namespace="Dominio.Modelo">
  <class name="Empresa">
    <id name="Id">
      <generator class="assigned" />
    </id>

    <property name="Nome" />
   
    <set name="Notas" inverse="true">
      <key column="IdEmpresa" />
      <one-to-many class="NotaFiscal" />
    </set>
  </class>
</hibernate-mapping>


NotaFiscal.htm.xml (child)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dominio" namespace="Dominio.Modelo">   
  <class name="NotaFiscal" >
    <id name="Id">
      <generator class="assigned" />
    </id>

    <property name="Serie" />
    <property name="Numero" />
    <many-to-one name="Empresa" column="IdEmpresa" />
  </class>
</hibernate-mapping>


To save and obj. "Empresa" with one "NotaFiscal" added I'm using the following code:

Code:
Empresa empresa = new Empresa();
empresa.Id = 2;
empresa.Nome = "xxxx";

NotaFiscal nota = new NotaFiscal();
nota.Id = 2;
nota.Serie = "PRE";
nota.Numero = 2;
nota.Empresa = empresa;

empresa.Notas.Add(nota);

NHHelper.Save(empresa); // Save and Flush


But when a look into the table "Empresa" and "NotaFiscal" just the first is saved!

Should I use the ISession.Save(nota) (without flush) before my Save(empresa)?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 3:15 pm 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
try adding cascase=all to your <set>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 6:52 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Oh God! Thanks :D

I forgot it! It should be default, I think. ;)

Bye.


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