-->
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.  [ 2 posts ] 
Author Message
 Post subject: HELP: saving related objects
PostPosted: Mon Jul 25, 2005 3:46 pm 
Newbie

Joined: Sun Jul 17, 2005 7:26 pm
Posts: 1
I am new for hibernate. I met a situation like this:
I have two classes: Parent and Chidren. Questions are:
1) When I insert a new child, the parent's id (primary key) is unknown and will be created automatically by "increment". Now I have this:
Parent p = new Parent();
p.setName("aha");
Children c = new Children();
c.setParent( p );
c.setName("sfs");

If I save/store c, can both p and c be stored in database? It seems I am only able to store c into database and id for p in Children table is null. I thought hibernate should be able to save c first and save p then.

2) About update. Say, I need to check if parent exists in database before I insert any child. But the checking is not based on Parent table's primary key but another field e.g. Parent's name. Should I do the following: a) search existing parent in database, b) set the found patient with new values c) save back to database. It sounds hibernate didn't help me too much on this. I don't know if I am using hibernate correctly. Any suggestion?

Thanks!

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
learning from the beginning


Top
 Profile  
 
 Post subject: refresh
PostPosted: Mon Jul 25, 2005 9:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Try refreshing your parent object:

Code:
// Set up Parent p and Children c, including c.setParent(p)

session.saveOrUpdate(p);
session.refresh(p);
session.saveOrUpdate(c);


I haven't quite figured when that's necessary and when it isn't, but it certainly won't hurt.


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