-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to control use of proxies
PostPosted: Sun Sep 21, 2003 12:16 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I have one complex entity Company and tons of small child entities. Each child entity mapping references parent Company. As result, Company was loading (using join) when I tried to load any child entity.

I have tried to specify outer-join="false" for the child mappings but this did not help - Hibernate continued loading parent Company but with separate select instead of join. (Correct?)

It seems the only way to prevent loading Company every time is setting "proxy" attribute in the company mapping. Is that true?

Unfortunately, after I have modified code to work with proxy, most of the test failed. This is because session.load() always succeeds even for non-existent ID - it returns proxy object where getId() works. The only way to force Hibernate raise exception is session.initialize();

What I need:
1. I need load Company in many places and I need it will be loaded immediately (no proxy).
2. I do not want Company be loaded when I load its child.
3. I do not want loading company with nonexistant ID succeed.

What is the best way to go?


Top
 Profile  
 
 Post subject: lazy loading
PostPosted: Mon Sep 22, 2003 12:43 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Try using lazy="false" in your mapping to force the object to load.
Alternatively you can do this in your code:
Code:
Hibernate.initialize(myObject)

to force that layer of the object mapping to be initialized "immediately" after you have loaded the object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2003 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
For 1 & 2, set up Company to use proxy. Then whenever you don't want it to load as part of a relationship, mark that relation using lazy="true" / outer-join="false"...

For 3, the only way is to not load using load. Rather, load using a query based on the id. I think Gavin added support for a new type of load method in one of the CVS versions, but I don't recall the specifics. It was named somthing other than load()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 10:03 am 
Newbie

Joined: Fri Sep 19, 2003 1:19 pm
Posts: 18
Steve,

To session.load() a lazy object I have used:

session.load( MyClass.class, myKey, LockMode.NONE );

and this seems to load the proxied object immediately.

I have no idea what is happening internally. I am not using any cache.

I did this for code consistency. I specify a lockmode, mosty NONE, for all my loads. This way I can avoid sometimes having to explain in my coding the

session.load();
session.initialize();

situation.

From your point of view, is this acceptable? Are there some internals that may trip me up in future revs?

Evan Vaala


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 11:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
That works also...


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