-->
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: question about accessing unmapped DB columns in query
PostPosted: Tue Feb 17, 2009 2:16 pm 
Newbie

Joined: Fri Feb 06, 2009 8:57 pm
Posts: 8
Hibernate version: Hibernate 2

Name and version of the database you are using: MySQL


Hi there,

When I'm doing the mappings of POJO properties to MySQL columns everything is running smoothly.

With a snippet of code like this I get a list with UserGroup objects:

Code:
         Query query = this.hibernate().getHibernateSession().createSQLQuery(
               "select {grp.*}, m.first_name " +
               "from ch_group {grp}, ch_member m " +
               "where m.is_group_admin = 1 and  {grp}.group_id = m.group_id ",
               "grp",
               chawk2.pojo.UserGroup.class);
         list = query.list();



Naturally I can access all POJO properties using UserGroup getters.
However, my challenge is retrieving additional values that I've added to SQL SELECT. Typical example is m.firstName above (not a property of UserGroup POJO).

Is there a way to access items in SELECT that are not in POJO and that aren't mapped? I was thinking, it would be convenient if I could add all these other items in SELECT to a Map which I can access via POJO a getter - [something like Map getMap()].

I am just not sure how would I go about implementing this.
If this is not possible, how do you usually handle this?

I can create a new class UserGroupReport with completely new mappings, but I'm thinking there has to be an easier way to deal with this type of problem. Can someone please steer me to the right direction?

The books that I've read are either too complex for a beginner or don't offer what I'm looking for.

Thank You,
Tim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 3:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you also need to call SQLQuery.addScalar() to make Hibernate aware of the additional column. Eg.

Code:
query.addScalar("m.first_name", Hibernate.STRING);


The result should be returned in a list with an Object[] in each element. The 0:th element is the UserGroup entity and the 1:th element is the name. It may be possible to change the way the data is returned by specifying a ResultTransformer (Query.setResultTransformer()) but I have no experience with that.


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Feb 17, 2009 6:11 pm 
Newbie

Joined: Fri Feb 06, 2009 8:57 pm
Posts: 8
Thanks for replying.

.addScalar() looks like a great solution. I wasn't aware that the UserGroup object would go in as 0th element. This is perfect.

Is there a way to do this in Hibernate 2 or am I going to have to upgrade to 3? .addScalar() method doesn't seem available in Hibernate2. I hope you can correct me if I'm wrong.

Thanks again in advance.

tim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Is there a way to do this in Hibernate 2 or am I going to have to upgrade to 3?



Oh... I didn't see that you were using Hibernate 2, and I have no idea what is or what isn't available in Hibernate 2.


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