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-customLooks 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 >= :startFrom and
hour < :endAt and
service_provider_id = :providerId and
mobileno = :number
group by mobileno, hour
order by mobileno, hour desc
</sql-query>