-->
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: Select, tuples and POJOs
PostPosted: Mon Mar 13, 2006 5:34 am 
Newbie

Joined: Mon Mar 13, 2006 5:26 am
Posts: 2
Hello!

I have class Foo with three fields: 'a','b','c' and methods: setA, getA, setB, getB, setC, getC. when I create query "select a,b from Foo" I get Objet[2] as a result with Object[0]=a, and Object[1]=b. What I would like to achieve is to get instance of Foo with 'a' and 'b' fields properly set and empty 'c' (after query.iterate().next())

How am I supposed to do it?

Best Regards
Boloek


Top
 Profile  
 
 Post subject: Use lightweight class pattern
PostPosted: Mon Mar 13, 2006 1:57 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
The only reason I could think one would want to slice an object like that is to avoid initializing the third field ("c" in your example) to save memory. If this is what you are trying to do, here is a link to the Lightweight Class pattern description - this pattern solves this problem by creating a lightweight superclass: http://www.hibernate.org/41.html.

Note that this pattern is useful only with Hibernate 2.x - you can achieve the same effect effortlessly and without the negative consequences in 3.x by using field-level lazy initialization. Using your example, you'd map "c" for lazy loading, and issue a query for the whole object. Hibernate 3.x will issue a query for a subset of fields that you didn't mark for lazy loading, giving you an object with only "a" and "b" set. Moreover, the code using the object will not notice that "c" is empty because it will be loaded on demand when you call getC().

I hope this helps.


Top
 Profile  
 
 Post subject: Re: Use lightweight class pattern
PostPosted: Tue Mar 14, 2006 3:26 am 
Newbie

Joined: Mon Mar 13, 2006 5:26 am
Posts: 2
skalinic wrote:
The only reason I could think one would want to slice an object like that is to avoid initializing the third field ("c" in your example) to save memory.


actually I was thinking about performance issues. I have db table with 120 fields and I must scan it checking only few values. There are also extreme conditions when all values I use are indexed, so db engine does not have to get table data (only index values)
second thing is code/configuration bloat and possible mistakes when I have to put returned tuples into POJOs so I can write code: 'if (a.getValueX().equals(b.getValueY())', instead of meaningless 'if (taba[5].equals(tabb[3]))'
third: it is easy (in my approach and lazy loading one) to add field 'Foo' in HQL's select and right away use it few lines below by simple getFoo() statement. Though, lazy loading will need extra configuration...
Also I am afraid of "loading at demand" because I exactly know (at the moment of HQL statement) which fields I'll use so there is no need for any extra db queries

Please note that I'm new to hibernate :-)


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.