-->
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.  [ 13 posts ] 
Author Message
 Post subject: Returning a non-entity from a named query, return-scalar,etc
PostPosted: Mon Mar 13, 2006 10:15 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:10 pm
Posts: 30
Hibernate version: 3.1.2

I want to return a non-entity from a named query. That is, the returned objects from a named query are non-entities in the sense that I do not need to persist them. That is because, the named query does an aggregate function (e.g. select sum(), etc... )

However, I am not sure on how to go about this.
Reading the Reference Manual, I looked at:

-------------------------------------------------
1) return-scalar

http://www.hibernate.org/hib_docs/v3/re ... medqueries

The link above talks about using <return-scalar>, using the following example:

Code:
<sql-query name="mySqlQuery">
    <return-scalar column="name" type="string"/>
    <return-scalar column="age" type="long"/>
    SELECT p.NAME AS name,
           p.AGE AS age,
    FROM PERSON p WHERE p.NAME LIKE 'Hiber%'
</sql-query>


But it does not exactly say how do you use it in code. That is, what Java type is returned for a single row when using <return-scalar> ??

-------------------------------------------------
2) Custom-value types

http://www.hibernate.org/hib_docs/v3/re ... pes-custom

Looks like what I need, but ... the link above shows how to you custom-value types for __properties within a defined entity__.


-------------------------------------------------
How should I proceed about it ?

Just to show what I was trying to do, see the snippet from a mapping.
It throws an exception saying "Unknown entity: com.mig.connectivity.hibernate.UserMessageAggregate", which is right ... because it is indeed not an entity ... and I don't want it to be an entity.

Code:
      <sql-query name="com.mig.connectivity.hibernate.getUserMessageAggregate">
       <return alias="userMessageAggregate" class="com.mig.connectivity.hibernate.UserMessageAggregate">
           <return-property name="mobileNumber" column="mobileno"/>
           <return-property name="hour" column="hour"/>
           <return-property name="totalMo" column="total_mo"/>
           <return-property name="netMt" column="total_mt"/>
           <return-property name="sentMt" column="sent_mt"/>
       </return>
       select mobileno, hour, total_mo = sum( total_mo ), net_mt = sum( total_mt ), sent_mt = sum( sent_mt )
       from player_gametype_aggregate
       where
         hour &gt;= :startFrom and
         hour &lt; :endAt and
         service_provider_id = :providerId and
         mobileno = :number
       group by mobileno, hour
       order by mobileno, hour desc
    </sql-query>



Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 10:30 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
1) Each entry in the returned list is an Object[] big enough to store all the scalars. So for mySqlQuery, you'd get a list of Object[2], where Object[0] is a String containing name, and Object[1] is a Long containing age.

2) If you want to return an object that isn't an entity, then you can't use a return alias. You can either return the Object[] as above, or use HQL's select new functionality. That only works if all the columns you're selecting from are mapped in entities. It's also only in v3.1, afaik. It's described in section 14.5. "The select clause", of the 3.1 ref docs.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 3:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It is not possible in 3.1 to return anything else than scalar, components and entities via native sql.

that said i have implemented support for ResultTransformer's to native sql and HQL in Hibernate 3.2 which will allow you to do this and more (but it is not yet committed to svn...but soon ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: A small follow-up
PostPosted: Tue Mar 14, 2006 4:37 am 
Newbie

Joined: Fri Nov 04, 2005 4:47 am
Posts: 14
I currently have the very same need. I have this fairly big SQL query (roughly 110 lines) which computes various statistics using cross-tabulations and stuff like that. The result of the query is 22 different numbers. To simplify handling of this I've created a "Benchmark" object which is a very dump POJO with a bunch of getters and setters. To process the query I have to do something like this:

Benchmark benchmark = new Benchmark();
benchmark.setCountByTech(((Double) result[0]).intValue());

The second line there will have to be repeated for the 22 different numbers, with only some variance on the set method being called and the index used. It would be incredibly cool if I could define this column-to-attribute mapping in the sql query (in the mapping file), and have the named query return arbitrary objects. If I could simply say that my named SQL query returns "Benchmark", and named the result set columns the same as the Benchmark properties, then all the mapping should happen automagically. My current project involves a lot of statistical queries, and since these are not persistent entities, Hibernate seems to get in my way.

I can also imagine that I would be neat (at least for some quick-and-dirty cases) to say for example that the named sql query returns a Map, much like the queryForMap/queryForList methods in Spring work. This sort of thing is indispensable for executing arbirary queries and quickly inspecting the results, and while I don't want that sort of code in my production system, it is very nice to have in my test code where I frequently need to use simple queries to verify the results of more complex queries.

Will your ResultTransformer support somethine like this?

If not, are there plans for something like this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 4:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i think so ;)

Here is some examples based on having a query like the following (remember this is work in progress so maybe this api will change sligtly but the concept/result will be the same):

query = s.createSQLQuery("select x as name, y as age from something");

Then

Code:
query.setResultTransformer(Transformers.TO_MAP).list()


will return Map's with entries for "name" and "age"

Code:
query.setResultTransformer(Transformers.toBean(PersonDTO.class))


will return instances of PersonDTO on which setName(String) and setAge(Long) have been called.

And this will work for both sql and hql (you can get Map, List and constructor beans today. ResultTransformer is a more generic way
of doing "select new xxx" in HQL)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 6:05 am 
Newbie

Joined: Fri Nov 04, 2005 4:47 am
Posts: 14
Sweet! I hope you'll have this release really soon :) I'd love to ditch those last pieces of Spring code in favour of an all-Hibernate setup. Fewer jars, fewer ways of doing things, simpler code. Happy happy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 6:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
its targetted for hibernate 3.2

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 4:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's times like this I'd like to be able to rate in other people's topics. Someone heap praise on max for me :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I could edit my points and give myself a heap of points from you, but somehow that doesn't sound fair ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 5:05 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Good grief max, have you been at work for over 11 hours? You should really buy a playstation or something.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 5:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i got one but it never praises me ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 1:27 am 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:10 pm
Posts: 30
That new feature with "select new()" was very powerful.

Now what if I wanted to return non-entities from a stored procedure call as well ?

The example in the online docs:

http://www.hibernate.org/hib_docs/v3/re ... l#sp_query

Code:
<sql-query name="selectAllEmployees_SP" callable="true">
    <return alias="emp" class="Employment">
        <return-property name="employee" column="EMPLOYEE"/>
        <return-property name="employer" column="EMPLOYER"/>
        <return-property name="startDate" column="STARTDATE"/>
        <return-property name="endDate" column="ENDDATE"/>
        <return-property name="regionCode" column="REGIONCODE"/>
        <return-property name="id" column="EID"/>
        <return-property name="salary">
            <return-column name="VALUE"/>
            <return-column name="CURRENCY"/>
        </return-property>
    </return>
    { ? = call selectAllEmployments() }
</sql-query>


... works if Employment was an entity.

What if I wanted to return a non-entity from a stored procedure call within a named query in my mapping file ? ( I want all queries externalised into the mapping files ). From the looks of the docs and the replies in here ... it looks like it is not possible at the moment ... except for perhaps using Object[]. The online docs also says: "Notice stored procedures currently only return scalars and entities".

Is there a possibility to have this in a future release ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 1:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the "select new" have been there for a loong time....the new thing is resultTransformer which will be in 3.2 and it will to return nonentities from stored procedures.

currently no plan for specifying it in the named queries, but you can always apply a resulttransformer in code, also on named queries.

_________________
Max
Don't forget to rate


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