-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to save Parent object given the Child object
PostPosted: Thu Oct 13, 2005 9:38 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
Hello

I am writing a generic program which need to retrieve every dependent (both parent and child) records given a value of a filed from any table.
E.g: Given user_id, I need to retrieve the group(parent objet) and user_details(child object). The "big object" is then transfered to a second detabse using session.replicate().

I don't have any problem starting from the parent and retrieving the childs, and afterwards saving the object containing every dependant table to another database "on the fly".

The problem is that when i start from the child, i am unable to insert/update the parent objects in the second database. Only the childs get persisted.

any idea?
Thx

PS:
In the Parent hbm (Group):
<!-- bi-directional one-to-many association to User -->
<set
name="users"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="GROUP_ID" />
</key>
<one-to-many
class="com.warfrog.hibernatetutorial.hibernate.User"
/>
</set>

In the child hbm (User):
<!-- bi-directional many-to-one association to Group -->
<many-to-one
name="group"
class="com.warfrog.hibernatetutorial.hibernate.Group"
not-null="true"
>
<column name="GROUP_ID" />
</many-to-one>

Hibernate version:
3

Code between sessionFactory.openSession() and session.close():
Session session = getHibernateSession()
Query getFrost = session.createQuery(
" select distinct u from User u where u.userId =236" );
List result = getFrost.list();

Group gr = new User();
gr = (Group)result.get(0);
session.close();

session = getHibernateSession2();
System.out.println("Uploading to new DB");
Transaction tx = session.beginTransaction();
session.replicate(gr,ReplicationMode.OVERWRITE);
tx.commit();
session.close();
Name and version of the database you are using:
HSQLDB


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 1:35 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
To simplify my question:

Can I load the parent object directly by a simple query from the child table?
Eg: "select distinct u from User u where u.userId =236"

Can the parent (e.g Group) be obtained without, say "u.getGroup()"?

The stack shows only request from the childs (e.g select from user, select from user_details etc) and not from the parent)

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 7:25 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Why not just get the group? If you want to replicate the entire group, then isn't that exactly what you want?

I'm not understanding why you don't just get the single group, then initialize the collection of users (so they all get loaded), then replicate the session. That seems like exactly the right thing, so why are you even trying to go from the child to the parent?

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 12:29 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
Thanks for the reply.

In fact the group user relationship was only an example. I need to write a more generic function. given the value of a field in any table, I need to retrieve all dependencies and parents. In this example it's the user_id that and have in have and not the group. I need to get the parent(s) which an sql like "select u from user where user_id=233".

Is it possible, or is there some limitations I should do with?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 12:45 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Now I'm very confused.

"Given the value of a field in any table"? How do you know what the dependencies and parents from an arbitrary table *are*?

How would you write your program in plain Java / JDBC, *without* Hibernate? If you don't know, then Hibernate won't really help :-)

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 5:26 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
OK,

lets take a simple case:
I can get the childs from a parent by adding "lazy =false" in the child set of the parent when querying by "select g from Group g where g.id=20".

Can i get the parent loaded by a simple query to the child: "select u from User u where u.id =50"?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 1:16 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Did you *try* it?

What happened?

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 1:37 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Kova_Sama wrote:
OK,

lets take a simple case:
I can get the childs from a parent by adding "lazy =false" in the child set of the parent when querying by "select g from Group g where g.id=20".

Can i get the parent loaded by a simple query to the child: "select u from User u where u.id =50"?


add fetch="join" to the many-to-one back to group.

In the child hbm (User):
Code:
<many-to-one name="group" class="Group" not-null="true" fetch="join">
  <column name="GROUP_ID" />
</many-to-one>

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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