java 如何从 HQL 中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2021988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:58:23  来源:igfitidea点击:

How to select from HQL

javahibernateselecthql

提问by Patrick Nevin

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

我是 HQL 的新手,有一个需要转换的 SQL 表达式,但无法选择 SQL 语句是:

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:

我可以毫无问题地在 HQL 中运行这部分:

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

However I only want the SenseDateand SenseValuevalues returned, could someone show me how to select as when I try to add select SenseDate, SenseValueetc. I keep getting errors in Netbeans

但是,我只希望返回SenseDateSenseValue值,有人可以告诉我如何在尝试添加时进行选择select SenseDate, SenseValue等。我在 Netbeans 中不断出错

回答by whiskeysierra

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

您可以使用 HQL 选择字段/列。看起来像下面这样:

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.

列表中的每个元素代表一个找到的行。数组本身包含两个选定字段,其顺序与您在语句中声明的顺序相同。

回答by Frederik Gheysels

You'll have to use something like projections.

您将不得不使用诸如投影之类的东西。

This means, that you'll have to create a class that will hold the results that you're trying to retrieve.

这意味着,您必须创建一个类来保存您尝试检索的结果。

In your case, this could 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; 
    }
}

Then, you'll have to let NHibernate know about the existence of this class. You can do that, by 'importing' it. So, this means you do not have to create a mapping file for it, you just have to do this in a hbm file:

然后,您必须让 NHibernate 知道这个类的存在。您可以通过“导入”来做到这一点。因此,这意味着您不必为其创建映射文件,您只需在 hbm 文件中执行此操作:

<import class="myclassname" />

And then, you can just do this in HQL:

然后,您可以在 HQL 中执行此操作:

select new SenseUserData (SenseDate, SenseValue) from UserData

回答by dotjoe

I think HQLhas the "new" keyword now.

我认为HQL现在有了“new”关键字。

select new Family(mother, mate, offspr) from Eg.DomesticCat as mother join mother.Mate as mate left join mother.Kittens as offspr

从 Eg.DomesticCat 中选择新的 Family(mother, mate, offspr) 作为母亲加入 Mother.Mate 作为 mate left 加入 Mother.Kittens 作为 offspr

Similar to projections/resulttransformers in the Criteria world.

类似于标准世界中的投影/结果转换器。

回答by Ruggs

It looks like you need to do what hibernate calls projections. Here is some info on how to do projections in HQL:

看起来您需要执行休眠称为投影的操作。以下是有关如何在 HQL 中进行投影的一些信息:

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

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