-->
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: Partially populating object instance from a query
PostPosted: Tue Jan 20, 2004 3:25 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
Is there any built in mechanism to do a HQL query and return a List of partially populated object instances?

For example, say if I have a Person class with firstName, lastName, dob, sex, height attributes.

There may be a case where I only want to return the firstName and lastName fields for every Person, i.e. at this point in time I am not interested in retrieving Person instances with every attribute populated.

If I issue a query like this:

Code:
List personList = session.find("select person.lastName, person.firstName from Person as person");


...if I understand this correctly this will return to me a List of Object arrays, one Object array for each row returned, where each Object in the array represents the selected data from the query.

I think overall this makes sense, as HQL queries objects, not actual database columns right? If I select an attribute on my Person class which is say of a PersonName type, then I would retrieve PersonName instances right?

So my original question, is there any way in which I can retrieve partially populated instances from a query?

Thanks,
Kevin Hooke


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2004 5:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
you can use "select new Person(person.name, person.firstname) from Person person" probably, if you have an appropriate constructor.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 1:27 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hi,

In most cases the performance hit (in terms of the database) for accessing the tuple for multiple columns as opposed to a single column in the same table is negligible. One of the exceptions is when you have (binary/character) large objects in the db which you need to stream.

On the vm side the picture is slightly different: are you concerned about creating large java objects which may consume a larger amount of memory?

A pattern that may be of interest to you is the Lightweight class: http://hibernate.org/41.html

Otherwise gloeglm's suggestion may be a simpler solution.

Regards


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.