-->
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.  [ 12 posts ] 
Author Message
 Post subject: whats a proxy?
PostPosted: Mon Oct 06, 2003 6:09 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
im confused, in the docs it says that this is a proxy:

proxy (optional): Specifies an interface to use for lazy initializing proxies. You may specify the name of the class itself.

how and when should I use this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
When
Quote:
The optional proxy attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked. See "Proxies for Lazy Initialization" below.

If you want an object to be really loaded when you first access a method of it , use proxy.
Code:
myObject = session.load(...);
myObject.getValue();

The object will be loaded when getValue is accessed (not 100% sure of it, but should be true).

I've never used it in real life but with collection. Lazy loading of collection is enabled by
Code:
lazy="true"
in collection mapping. No need for proxy parameter.

How
Quote:
You may specify the name of the class itself.
is the easiest way. Your class should not be final.
For collection add
Code:
lazy="true"
in collection mapping.

To sumarize, don't bother with proxy parameter.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 9:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
In essence, a proxy is to single-point associations what lazy is to collection associations. It is simply a way to defer loading an associated object until it is needed (if ever).

For proxies, Hibernate dynamically generates a class which represents your object. It follows the interceptor type pattern. When you access an attribute of the proxied class (other than the id), the Hibernate-generated proxy will load the underlying data from the database. It is somewhat akin to the java notion of proxy (java.lang.reflect.Proxy).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 2:37 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
so where do you guys use proxys in apps?

I cant seem to think of a usecase where I would need to use it (yet), if I load the object its because I need to use it immediately


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 3:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Every single one of my domain entities has a Hibernate proxy defined for it. So I use it all over the place.

Without using proxies, all many-to-one associations will be loaded as soon as the association owner is loaded.

Also, say a user on a web screen chooses som drop-down value as part of a form. In processing that form, you need to set an association on some class based on selected id value from the drop down. So, you'd have to do something like:
Code:
    AssociationOwner owner = (AssociationOwner)session.load(AssociationOwner.class, ownerId);
    OwnedAssociation assoc = (OwnedAssociation)session.load(OwnedAssociation.class, userChosenId);
    owner.setAssociation(assoc);
    session.flush();


Without the use of proxies, the above statements would cause two select statements, plus the update statement at flush time. If you utilize proxies, Hibernate can skip loading the OwnedAssociation class...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 5:40 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
would it be safe to say I should always use proxys in a web-enviroment?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Depends the query done whth your objects. Here is an example of lazy loading overhead

Code:
owner = (Owner) session.load(Owner.class, new Long(1));
assoc = owner.getAssociation();
assoc.getName();

You'll have 2 queries instead of 1.

As steve points out, association updates, used by web forms update use cases, are optimized with lazy loading.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
FWIW,
You would have 2 queries there regardless of proxy usage/non-usage.


Top
 Profile  
 
 Post subject: Proxies
PostPosted: Mon Oct 06, 2003 7:15 pm 
Newbie

Joined: Wed Sep 03, 2003 11:30 pm
Posts: 5
Location: Minneapolis, Minnesota, USA
For a description of proxies, go to the Documentation tab, under Hibernate2 Documentation. In the Hibernate2 Reference Manual. There is a description of Proxies in section 11.1.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 3:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
steve wrote:
FWIW,
You would have 2 queries there regardless of proxy usage/non-usage.

Despite usage of outer join ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 10:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
outer-join just tells Hibernate to go ahead on initialize the association even if there is a proxied assocation. So yeah.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
outer-join="true" means use an outer join, regardless of proxies
outer-join="false" means never use an outer join, regardless of proxies
outer-join="auto" means use an outer join, unless there is a proxy


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