Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when save parent, the children do not get saved

 
leela penmatsa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question and it could be a very simple/silly question for you. Please reply back to my post immediately as soon as possible. Please treat this as very urgent. Thanks a lot for the help in advance.

When I save a parent, the children do not get saved, unless i save them independently. Is there anyway that i can bypass saving children independently? I just wanted to make one call to save parent which should take care of saving children as well. please guide me. This is a unidirectional one-to-many mapping.


<hibernate-mapping>
<class name="Parent" lazy="true" proxy="Parent" table="parent">
<cache usage="read-write"/>
<id name="id" access="field" type="long" >
<generator class="MyGenerator"></generator>
</id>
<!-- some properties here -->
<set name="children" cascade="all,delete-orphan"
access="field"
table="child">
<cache usage="read-write"/>
<key column="parentid" not-null="true"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>

here is the code :

Session session1 = sf.openSession();
Parent parent1 = (Parent )session1.load(Parent.class, 123);
parent parent2 = new Parent(parent1);
session1 .flush();
Session session2 = sf.openSession();
session2 .save(parent2);
session2 .flush();

Please let me know what is wrong here. Thanks a lot.
[ October 25, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"leelaaaa leelaaaa", please check your private messages for an important message from me.

Also, you might want to read this for information on why putting URGENT in your post is not a good idea. I have removed the term from your title for you.
 
leela penmatsa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have updated my name in the profile, i do not know why the name is not getting reflected on my posting.

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any children at all in the code you posted, just one Parent and two references pointing to the same Parent object.

Also, I noticed you opened two Sessions, never start a transaction, and call flush() instead.

Each Session will have its own persistence context, so it is possible that the Parent is being managed by one session, while the child is being managed by the second session.

I also notice you called load() which in your case will only return the Parent object and not the child.

Usually you do this. The relationship mapping has the cascade option like "all"

so


Mark
 
leela penmatsa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark : I don't see any children at all in the code you posted, just one Parent and two references pointing to the same Parent object.

Leela : I am copying all the properties from one parent that is returned by the hibernate and creating another parent object to persist that later.
Here is the code in the constructor.
public Parent(Parent another) {
this.setName(another.getName());
this.children = new HashSet();
for(Child anotherChild : another.getChildren()) {
Child child = new Child();
child.setName(anotherChild.getName());
children.add(child);
}
}


Mark : Also, I noticed you opened two Sessions, never start a transaction, and call flush() instead.

Leela : That means, i should only use one session to load and recreate the same object? Please clarify me here once again. I did not get this statement at all. thanks.


Mark : Each Session will have its own persistence context, so it is possible that the Parent is being managed by one session, while the child is being managed by the second session.

I also notice you called load() which in your case will only return the Parent object and not the child.

Leela : I am able to see the children from the parent1. Please clarify me here once again. thanks.

Mark : Usually you do this. The relationship mapping has the cascade option like "all"

Leela : I have a cascade option with 'all'. have i missed another from your statement here.

Leela : What i am trying to do is to get an entity including children from database and then recreate the object and then persist again. I am not sure why the hibernate is not letting me do to persist the same object another time. I am having problem when children are getting saved. I was getting the
StateStaleException : Batch update.. row count etc..

Any ideas why this is happening? Thanks a lot for the reply. I really appreciate that.

so

code:
--------------------------------------------------------------------------------

Session s = SessionFactory.openSession();Transaction t = s.beginTransaction();try { Parent p = s.load(Parent.class, 123); //If there is child in the database already { Child c = p.getChild(); } //or if no child { Child c = new Child(); p.setChild(c); } c.setChildProperty("SomeValue"); session.save(p); t.commit;} catch (Exception e) { t.rollback();} finally { session.close();}

--------------------------------------------------------------------------------



Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic