-->
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: Proxy use with Subclasses, trying to avoid extra overhead
PostPosted: Wed Apr 06, 2005 3:25 pm 
Newbie

Joined: Thu Apr 29, 2004 11:40 am
Posts: 11
Trying to figure out how to use Proxies - I've read all the information I can find but I'm still abit clueless. I get they are ways of lazy-loading so I don't have to instantiate my objects with every attribute (i.e. save sql calls).

Here's the scenario I'm trying to optimize:

I'm making a call to viewObject?id=5 where the id is the uid of the ClassA in the db (classA.getId() = 5).

I want to do something along the lines of:
ClassA classA = session.load ( ClassA.class, id );
Set attachments = classA.getAttachments();

I don't care to retrieve *any* of the actual properties for ClassA - just want to use the known Id to retrieve the attachments.

I could obviously do this in SQL as :
select * from tbAttachments where classId=5
but that's not very Hibernaty.

Now this would be easy if it wasn't for the fact that ClassA is extended by ClassB, ClassC, and ClassD. I frequently need to cast between these classes throughout the application. The combination of needing to be able to cast and wanting to use Proxies seems to be giving me a time.

Hibernate version: 2.x


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 5:33 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
I think you have 2 options.

One being to set lazy=true in the class hbm.xml. This is when the proxy is the class itself.

I believe then you'd need to call Hibernate.getClass() to retrieve the actual class. I can't recall how this affects the casting though.

If you need to do casting, for each class you want to proxy, specify lazy=true and the proxy interface to be a interface matching all the methods of the class itself. I just extracted interfaces using Eclipse. I believe this will work for casting.


Top
 Profile  
 
 Post subject: Using Interfaces allows Casting?
PostPosted: Wed Apr 06, 2005 9:18 pm 
Newbie

Joined: Thu Apr 29, 2004 11:40 am
Posts: 11
I couldn't tell from the Documentation rather the Interface solution proxy="IClassA" fixes both the inability to cast subclassed proxies and the ability to compare class references

http://www.hibernate.org/hib_docs/refer ... ce-proxies

**Casting example where Proxy is the class itself
----------------------------------------------------------
Cat cat = (Cat) session.load(Cat.class, id); // instantiate a proxy (does not hit the db)
if ( cat.isDomesticCat() ) { // hit the db to initialize the proxy
DomesticCat dc = (DomesticCat) cat; // Error!
....
}

**Interface Casting example
----------------------------------------------------------
ICat cat = (ICat) session.load(Cat.class, catid);
Iterator iter = session.iterate("from cat in class eg.Cat where cat.name='fritz'");
ICat fritz = (ICat) iter.next();

Anyone have any ideas if I will be able to cast the ICat to an IAnimal and back and forth with the interfaces?


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.