-->
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.  [ 3 posts ] 
Author Message
 Post subject: Fundamental Hibernate Question
PostPosted: Sun Jul 11, 2004 7:29 pm 
Newbie

Joined: Sun Jul 11, 2004 7:24 pm
Posts: 1
I have a fundamental question about how to use Hibernate.

I have a class Group with a many-to-one relationship with Person. The group class is an agregation composed of various class attributes, one being a User. When the datatbase scripts are generated the resulting entities are Group with a foriegn key named User_Id (Long) and of course User.

If I want to create a new Group and I know the ID of the user that will be associated with that Group, what are my options for saving the Group? From what I can see, I have no option but to first do a load of the User using the user_id, then associate the user object with the new Group object, and then save it. Is there a better/more efficient way of doing this? Shouldn't I be able to simply insert the new Group with the appropirate User_id into the database? Are there best practices around this, or am I doing it just as it should be done?

Thanks for your help!
Aaron.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 11, 2004 7:54 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In that case, you can use a nice trick: Just create a new dummy User with the ID you have, set it on the Group, save the Group. Hibernate will only look for the ID in the User object and use it to save the Group. Make sure you don't cascade any operations on the association from Group to User.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 12, 2004 10:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hum, not that the *best* way to create a "dummy" user is to enable proxies and do session.load(User.class, id), which does *not* hit the db.


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