How to choose from HQL

I am new to HQL and have an SQL expression that I need to convert, but I cannot select the SQL statement:

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 need to return the values โ€‹โ€‹of SenseDate and SenseValue , can someone show me how to choose, as when trying to add select SenseDate, SenseValue , etc. I keep getting errors in Netbeans

+6
java select hibernate hql
source share
4 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 doing this, you get a list of arrays of objects:

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

Each item in the list represents a found string. The array itself contains two selected fields in the same order that you specified in the instruction.

+5
source share

I think HQL now has a "new" keyword.

choose a new family (mother, assistant, offspr) from Eg.DomesticCat as a mother Join your mother. leftists join the mother. Kittens like offspr

Similar to projections / resulttransformers in the criteria world.

+4
source share

You will need to use something like projections.

This means that you will need to create a class that will contain the results that you are trying to get.

In your case, it might look something like this:

 public class SenseUserData { public DateTime SenseDate { get; private set; } public Decimal SenseValue { get; private set; } public SenseUserData( DateTime date, Decimal value ) { this.SenseDate = date; this.SenseValue = value; } } 

You will then need to tell NHibernate that this class exists. You can do this by importing it. Thus, this means that you do not need to create a mapping file for it, you just need to do this in the hbm file:

 <import class="myclassname" /> 

And then you can just do it in HQL:

 select new SenseUserData (SenseDate, SenseValue) from UserData 
+4
source share

It sounds like you need to do what hibernate causes projection to do. Here is some information on how to do projections in HQL:

http://www.devarticles.com/c/a/Java/Hibernate-HQL-in-Depth/1/

+1
source share

All Articles