-->
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: A way to return list of myObject instead of list of Object[]
PostPosted: Sat May 08, 2004 10:04 pm 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
When doing
query = session.createQuery("Select a.name,a.adress from myObject a");
List myList= query.list();

I will get a list of Obect[] objects, and not a list of myObject's.
Is there a way to make hibernate return a list of myObject , when I choose not to select all of the fields?

Or must I manually iterate over the list that is beeing returned, and re-create a list of myObject objects myself?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 09, 2004 12:37 am 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
You could always try adding a constructor for myObject that takes those fields as parameters, and call:

query = session.createQuery("Select new myObject(a.name, a.adress) from myObject a");
List myList = query.list();

On the other hand, you should probably decide whether that fine a degree of lazy loading is really any more performant than just loading the whole object. Being able to specify Object and Collection associations (one-to-one, many-to-one and one-to-many) is usually sufficient for most cases.

Scott


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 09, 2004 1:02 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
In my case, I will use hibernate in cooperation with struts in a web.app.
The reason I do not want to select the whole objects is for different reasons:

In those cases where I need to return a list of objects, that list will be used in adminstration of objects, that means editing and deleting objects in the database. I only need things like name and description in order to create a list of this.
Some of the fields in the tables contain large amounts of text that I do not need in order to create a listing of available objects.
If I need to display a list of 10000 objects, it would take longer to query and return all fields/whole objects than just a few fields/parts of the object?

Another thing is that sometimes I need to write the whole List to session, and I would like to use as little resources as possible.


Top
 Profile  
 
 Post subject: another thing
PostPosted: Sun May 09, 2004 1:45 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
Your solution seems to work excellent, however I have a question:
I had an idea to create constructor that took all of properties available for this class.

Then I could set the un-needed propterties to "null" or "0", and I would not need to create one constructor for each "case".

I tried doing this:
session.createQuery("Select new myObject(a.name, a.adress,null,0) from myObject a");

It does not seem to understand "null" and "0", so what do I need to do in order to make this work?


Christian H.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 09, 2004 2:36 am 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
As far as I know, HQL only supports selecting whole objects and object fields, not constants.

You could experiment with the use of createSQLQuery() to see if that works, but I don't know if it would.

It sounds like you want to support a situation where the fields to be loaded are dynamically specified (eg. by user preferences). In such a case, I wouldn't bother with the 'Select new ... ' pattern. Instead, just select the fields you want as you were before, step through the list of results and convert each Object[] into the object type you want. Do it within the data-access layer and it will all be transparent to other layers.

Scott


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.