-->
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.  [ 4 posts ] 
Author Message
 Post subject: ObjectProjection?
PostPosted: Wed Aug 16, 2006 6:08 pm 
Newbie

Joined: Wed Aug 16, 2006 5:52 pm
Posts: 5
I've been using the HQL dynamic instantiation in my listing methods.
For example:

session.createQuery("select new Summary(d.id, d.name, d.shortDesc) from ...");

Summary is a shortened version of the full class, used to get just the fields required to build a listing display and provide the key to the full objects. It is just a listing class and not intended to be a persistent entity.

Some of the listing queries I'm making have become more complex and dynamic so I've started using Criteria instead of Query. As far as I can tell, Criteria doesn't support the dynamic instantiation like HQL does (if I'm wrong about that please tell me). So instead I've used projection to get the subset of fields I'm interested in.
For example:

// Project just the properties required for summaries.
Projection properties =
Projections.projectionList()
.add(Projections.id())
.add(Property.forName("name"))
.add(Property.forName("shortDesc"));

// Create the selection criteria for the listing.
Criteria selectCriteria =
session.createCriteria(entityName)
.add(criterion)
.setProjection(properties);

Which generates a listing of Object arrays. So, I iterate over the results list and turn it into the Summary objects by doing:

List<Object[]> rawResults = selectCriteria.list();
List<Summary> results = new ArrayList<Summary>();

for (Object[] row : rawResults)
{
results.add(new Summary((Long)row[0], (String)row[1], (String)row[2]));
}

I've been wondering if there's a neater OO way of doing this than working with the array of objects. I'd be interested to hear any suggestions.

Do you think it would be possible/beneficial to have some kind of ObjectProjection class in Hibernate? This might work something like this:

// Project the summary class.
Projection summaryProjection =
Projections.objectProjection("new Summary(id, name, shortDesc)");

Hibernate would use this to extract the named fields and build the summary listing objects for me.

Rupert Smith


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 6:26 pm 
Newbie

Joined: Wed Aug 16, 2006 5:52 pm
Posts: 5
I think I've found the answer; its to use ResultTransformer. There is even a ready made one that maps alias names onto bean setter methods, AliasToBeanResultTransformer, that I could use.
For example:

// Project just the properties required for summaries.
Projection properties =
Projections.projectionList()
.add(Projections.id())
.add(Property.forName("name"))
.add(Property.forName("shortDesc"));

// Create the selection criteria for the listing.
Criteria selectCriteria =
session.createCriteria(entityName)
.add(criterion)
.setProjection(properties);
.setResultTransformer(new AliasToBeanResultTransformer(Summary.class))

So my ObjectProjection idea is unnecessary.


Top
 Profile  
 
 Post subject: Maybe use setFirstResult/setMaxResult?
PostPosted: Wed Aug 16, 2006 6:48 pm 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
Just wondering...

Have you tried loading the full objects but limiting the total number loaded in the session as follows?

session.createQuery("from ...").setFirstResult(0).setMaxResult(10);

Same idea applies if you need a dynamic query (like Query by Example). Have it return the full objects but limit the results.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 4:43 am 
Newbie

Joined: Wed Aug 16, 2006 5:52 pm
Posts: 5
Yes I'm using, .setFirstResult(...) and .setMaxResult(...), too but I just left it out of the examples that I gave. In some cases when retrieving the full object I also want to retrieve some associates of it, even collections, in order to build a detailed screen of information, so I thought having a Summary class for the listing screen would save on database traffic. You are right though, in many cases the full object is not much bigger than the Summary so I might as well fetch it all on the listing call.


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