-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with cascade="all-delete-orphan"
PostPosted: Wed Jan 03, 2007 8:36 am 
Newbie

Joined: Wed Nov 29, 2006 7:46 am
Posts: 9
Hi,

i have a stupid problem but i dont know how solve it , im sure nhibernate can do it but i dont why not do :)

I have a parent-child form, i read a parent from the db add a new child and then saveorupdate. I define the parent´s bag with cascade="all-delete-orphan" but when save this new child nhibernate try to save a null value in the parent´s reference in the child table and i have an error.

the code:

Dim alb As Albaran = albaranDao.GetById(1, False)
Dim albd As New Albaran_Detalle
albd.Id_Articulo.Id = 1
.....
Albaran_DetalleList.Add(albd);
albaranDao.SaveOrUpdate(alb)
albaranDao.CommitChanges()

error: No se puede insertar el valor NULL en la columna 'Id_Albaran', tabla 'ATENOR.dbo.Albaran_Detalle'. La columna no admite valores NULL. INSERT falla.Se terminó la instrucción.

cant insert null value in the column "Id_Albaran".... The column not admit null values. Insert fail....

Any know what is wrong??.

thanks for all.


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.Domain">
<class name="Albaran" table="Albaran">

<id name="Id" column="Id" type="Int32" unsaved-value="null">
<generator class="native"/>
</id>
<bag name="Albaran_DetalleList" inverse="true" lazy="true" cascade="all-delete-orphan" >
<key column="Id_Albaran" />
<one-to-many class="Albaran_Detalle" />
</bag>
<property column="Cod" type="String" name="Cod" length="50" />
<property column="Fecha" type="DateTime" name="Fecha" not-null="true" />
<many-to-one name="Id_Cliente" column="Id_Cliente" class="Cliente" />
<property column="Observaciones" type="String" name="Observaciones" length="50" />

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.Domain">
<class name="Albaran_Detalle" table="Albaran_Detalle">

<id name="Id" column="Id" type="Int32" unsaved-value="null">
<generator class="native"/>
</id>
<many-to-one name="Id_Albaran" column="Id_Albaran" class="Albaran" />
<many-to-one name="Id_Articulo" column="Id_Articulo" class="Articulo" />
<property column="Cantidad" type="Int32" name="Cantidad" />
<property column="Precio" type="Decimal" name="Precio" />
<property column="Descuento" type="Decimal" name="Descuento" />
<property column="Id_Factura" type="Int32" name="Id_Factura" />

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 9:52 am 
Newbie

Joined: Wed Nov 29, 2006 7:46 am
Posts: 9
maybe not is nhibernate works do it, im not sure.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 4:42 am 
Newbie

Joined: Wed Nov 29, 2006 7:46 am
Posts: 9
I found this post:
http://forum.hibernate.org/viewtopic.ph ... 0d70fd7f45

But im not sure yet, if i create a new parent with childs nhibernate saves all well(save the parent and then save the childs with the new parent´s id the only problem is when i add a new child to a existing parent.

any explanation about why it not save it?

thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 4:01 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Like the post that you found said, you need to handle the bidirectional relationship in your code.
So in addition to adding the child object to your parent's collection, you'll also need to assign the parent object to your child object's property (Id_Albaran in class Albaran_Detalle).
NHibernate doessn't handle the bidirectional relationship automatically, so I typically add methods on the parent (aggregate) objects that manage all this for me. Best practice.

Good luck!
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 4:24 am 
Newbie

Joined: Wed Nov 29, 2006 7:46 am
Posts: 9
thanks to confirm Mike.


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