-->
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.  [ 10 posts ] 
Author Message
 Post subject: Unidirectional - adding new child removes existing childs
PostPosted: Tue May 26, 2009 7:32 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
I got one-to-many relationship with Parent -> Child. Default cascade="save-update" in parent.

Parent.getChilds.add(Child1)
Parent.getChilds.add(Child2)

session.save(Parent)

Parent.getChilds.add(Child3)

session.update(Parent)

This works fine. But if I flush it before adding child3 then it creates new child3 but sets child1 & child2 parent to be null !!

So the following code removes the link from the parent.

Parent.getChilds.add(Child1)
Parent.getChilds.add(Child2)

session.save(Parent)
session.flush()
Parent.getChilds.add(Child3)

session.update(Parent)

Though the existing childs are not removed from the collection still hibernate tries to remove the link between parent and child !

I could fix this by making it to bi-directional and inverse="true" but due to requirement I can't do that. I will be happy if someone figures out whats happening here rather than giving alternate solutions.

Have added the mapping :

Parent Mapping

<hibernate-mapping default-cascade="save-update">
<class name="Parent" table="parent" dynamic-insert="false" dynamic-update="false">
<cache usage="nonstrict-read-write" />
<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="ID" sql-type="NUMBER(19)"/>
<generator class="native">
</generator>
</id>
<version name="version" type="int" column="version"/>
<set name="childs" lazy="true" fetch="select" inverse="false">
<cache usage="nonstrict-read-write" />
<key foreign-key="parent_CONTROC">
<column name="parent_FK" sql-type="NUMBER(19)"/>
</key>
<one-to-many class="Child"/>
</set>



Child Mapping

<hibernate-mapping default-cascade="save-update">
<class name="Child" table="child" dynamic-insert="false" dynamic-update="false">
<cache usage="nonstrict-read-write" />
<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="ID" sql-type="NUMBER(19)"/>
<generator class="native">
</generator>
</id>
<version name="version" type="int" column="version"/>



Thanks
K

_________________
Kumar


Last edited by rkumaresh on Tue May 26, 2009 12:12 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Tue May 26, 2009 11:23 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
Anyone ?

_________________
Kumar


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Wed May 27, 2009 10:04 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
Still no one :-(

_________________
Kumar


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 5:56 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
Any hibernate experts please?

_________________
Kumar


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 8:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
please show Parent.getChilds source, I guess you may be re initializing the collection each time.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 8:17 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
From Parent :

private java.util.Collection childs = new java.util.HashSet();

public java.util.Collection getChilds()
{
return this.childs;
}

public void setChilds(java.util.Collection childs)
{
this.childs = childs;
}

And because its unidirectional I have no mapping for Parent in Childs end, as you can see from the mapping in my earlier post.

Just to quick reminder it happens only when you flush and add new child.

_________________
Kumar


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 8:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are Child1,2,3 new instances or already persistent?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 9:15 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
All are new including parent.

_________________
Kumar


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 10:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
all are new inclusing parent and ouy don't save the children first? I don't see any cascading in your mapping file, how can that work?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: Unidirectional - adding new child removes existing childs
PostPosted: Thu Jun 04, 2009 10:38 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
If you have noticed in the class level, I do have default-cascade set.

And I did tried saving the new child before adding it to the existing collection but no luck.

May be I will make it more clear about the situation :

Parent p = new Parent ();
p.getChilds().add(newchild1);
p.getChilds().add(newchild2);

session.save(p);

session.flush();

Everything is fine up until now.

p.getChilds().add(newchild3);

session.update(p);

Issue:

Now is child3 is persisted and linked to parent but child1 & child2 parents are set to null !

And if I remove the session.flush() all seems fine - unfortunately in the application I can't do this for some reason.

_________________
Kumar


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