-->
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: mapping native sql results to non entity class
PostPosted: Thu Dec 02, 2010 2:40 pm 
Newbie

Joined: Wed Oct 20, 2010 11:22 am
Posts: 14
Is there a way to map a native query to a regular java class (not an entity).

I am returning data that is not an entity and would like to map to a class (not an entity). Is there a way to do this?

Thanks!


Top
 Profile  
 
 Post subject: Re: mapping native sql results to non entity class
PostPosted: Thu Dec 02, 2010 2:56 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
you can do it manually. Hibernate will return a list of object[], you just have to iterate over them.

Otherwise if your lazy like me you can use one of the ResultTransformers
http://swik.net/Hibernate/Hibernate+Gro ... d+SQL/cmxs
http://prasadmarrapu.blogspot.com/2008/ ... rnate.html

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject: Re: mapping native sql results to non entity class
PostPosted: Fri Dec 03, 2010 12:10 pm 
Newbie

Joined: Wed Oct 20, 2010 11:22 am
Posts: 14
OK, I'm really lazy like you.

I did get the setResultTransformer to work, but I still have one issue with it.

This is the code that I'm using
Code:
String query =
         "select tbl1.my_flag1 as myFlag1, " +
         "  tbl1.my_flag2 as myFlag2" +
         "from tbl1 where tbl1.id = :id";

      org.hibernate.Query q = ((Session)em.getDelegate()).createSQLQuery(query)
         .addScalar("myFlag1",Hibernate.STRING)
         .addScalar("myFlag2",Hibernate.STRING)
         .setResultTransformer(Transformers.aliasToBean(MyClass.class));
      q.setParameter("id", 1);
      q.list();


This doesn't work - it gives me a "column not found". It seems that it is returning the column name as all lowercase (using informix db). I would have assumed the alias' would have changed what the db names brought back.

If I change the following lines to "addScalar("myflag1")" / "addScalar("myflag2"), then it gives an error on my class saying it can't find a setter for the columns (the setters are setMyFlag1,setMyFlag2).

Is there an easy way to modify the column names brought back???

thanks!


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.