-->
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: Saving of raw foreign key
PostPosted: Tue Jan 06, 2009 6:27 pm 
Newbie

Joined: Wed Dec 17, 2008 4:15 pm
Posts: 4
Hi All,

Is there an easy hibernate method of saving a single new child using a raw foreign key? I have the foreign key from an earlier Criteria query (used to populate a pull down box) used for performance reasons.

I'm currently having to reload the parent and this is having a perfromance overhead. If I construct a transient parent object with only the key set, it sets the foreign key to null (and then fails upon insert with not null violation).

I don't really want to use JDBC :/

Happy to post details/example but didn't think it would be necessary...

TiA, rotis23


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 10:42 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
By default this will not go to the database:

Other other = (Other) session.load(Other.class, otherId);

Then you can set your association:

this.setOther(other);
session.save(this);

Pseudo-code, obviously ;)

The reason is that by default, load() will give you an uninitialized proxy containing exactly one field - the ID. Note that get() will always hit the database so it won't work for this situation. For JPA, use getReference().

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 5:50 am 
Newbie

Joined: Wed Dec 17, 2008 4:15 pm
Posts: 4
Thanks for the reply cbredesen.

For reasons I can't go into now the class is lazy="false" - so the full object is loaded during a 'load' operation.

I have the key but I need to load the entire object!

If there is no method to insert using the raw foreign key, is there a method of forcing a lazy load for a single load operation?

Thanks again.


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.