-->
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.  [ 1 post ] 
Author Message
 Post subject: Using createQuery to return partially populated beans
PostPosted: Wed Jan 19, 2005 3:08 pm 
Newbie

Joined: Wed Jan 19, 2005 2:26 pm
Posts: 1
We're attempting to select a DB peristence tool, so naturally my research has lead me here. I've never actually used Hibernate past playing around with the Caveat Emptor example, so please if I'm missing something, be sure to point it out.

The real issue we have is that we want both full and partial beaned results returned. I've noticed that when querying with HQL, if you choose to have SELECT return only a few columns, they come back as an array of Longs and Strings, for instance:

Code:
Query queryAuditOne = HibernateUtil.getSession().createQuery("select lr.message, lr.entityId, lr.userId from AuditLogRecord lr where lr.entityId = :id");


I found that you can force the result into a Java bean constructor, like such:

Code:
Query queryAuditOne = HibernateUtil.getSession().createQuery("select new AuditLogRecord(lr.message,lr.entityId,lr.entityClass,lr.userId) from AuditLogRecord lr where lr.entityId = :id");


But this is a clunky method to allow for any combination of selected columns, and inevitably can have two combinations that would match the same constructor imprint.

Here's the question: I've noticed that for a Criteria based query, you specify the class of the return table beaned object. For a query, I do not see such a format. Is there a way to FORCE a unique or list returned query to a specific type, and to set the properties of a bean when only partial results are returned?

For those who help, thank you! Can you please refrain from answering the question with the two following answers:
1) Redesign your tables so that this is not necessary and only full results are returned always.
2) On return of the List, use reflection to call beaned set methods of the object list. We're trying to avoid this.

Thanks all for the insight.

-ian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.