Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select from HQL

I'm new to HQL and have a SQL expression I need converted but am unable to select the SQL statement is:

select SenseDate as Time,SenseValue as Value
from UserData
where NetworkID = '23'
and IODeviceID = '129'
and SenseDate >=  DateAdd("d",-1, GETDATE())
and SenseDate<=GETDATE()

I can run this part in HQL without problems:

from UserData 
where NetworkID = '23'
and IODeviceID = '129'
and SenseDate >=  DateAdd(d,-1, GETDATE())
and SenseDate<=GETDATE()

However I only want the SenseDate and SenseValue values returned, could someone show me how to select as when I try to add select SenseDate, SenseValue etc. I keep getting errors in Netbeans

like image 961
Patrick Nevin Avatar asked Jan 07 '10 16:01

Patrick Nevin


1 Answers

You can select fields/columns using HQL. Looks like the following:

select
    SenseDate,
    SenseValue
from
    UserData
where
    NetworkID = '23'
and
    IODeviceID = '129'
and
    SenseDate >= DateAdd(d, -1, GETDATE())
and
    SenseDate <= GETDATE()

When you execute this you will receive a list of arrays of objects:

final List<Object[]> values = query.list();

Each element in the list represents a found row. The array itself contains the two selected fields in the same order you declared them in the statement.

like image 164
whiskeysierra Avatar answered Oct 01 '22 23:10

whiskeysierra