Java 如何从hibernate中的类型化查询返回非实体的对象列表?

Java 如何从hibernate中的类型化查询返回非实体的对象列表?,java,database,hibernate,Java,Database,Hibernate,我需要从实体类Employee中提取几个字段,添加几个额外的硬编码字段,并使用GROUPBY子句返回结果 下面是我尝试的代码: String query = "SELECT emp.category, emp.salary 0 as somevalue, 0 as dummy FROM employee emp " + "WHERE emp.date = :date AND emp.class = :class AND emp.classificationDet

我需要从实体类Employee中提取几个字段,添加几个额外的硬编码字段,并使用GROUPBY子句返回结果

下面是我尝试的代码:

String query = "SELECT emp.category, emp.salary  0 as somevalue, 0 as dummy FROM employee emp "
                + "WHERE emp.date = :date AND emp.class = :class AND emp.classificationDetail.shortDescription = :classificationType GROUP BY emp.category";

        TypedQuery<CustomEmployee> typQuery = entityManager.createQuery(query, CustomEmployee.class);

        typQuery.setParameter("date", req.getDate());
        typQuery.setParameter("class", req.getClass());


        return typQuery.getResultList();
String query=“选择emp.category,emp.salary 0作为somevalue,0作为employee emp中的伪值”
+“其中emp.date=:date和emp.class=:class和emp.classificationDetail.shortDescription=:classificationType GROUP BY emp.category”;
TypedQuery typQuery=entityManager.createQuery(查询,CustomEmployee.class);
typQuery.setParameter(“date”,req.getDate());
setParameter(“class”,req.getClass());
返回typQuery.getResultList();
但我遇到了一个异常,无法使用请求的结果类型为具有多个返回的查询创建TypedQuery

如何做到这一点。
谢谢。

首先检查这部分:
emp.salary 0 as somevalue
。这应该是
emp.salary as somevalue
0 as somevalue
,但不能两者都是

定义如下所示的类(保持简短;我使用公共属性,但如果需要可以更改):

用户可以按如下方式在查询中使用它:

String query = "SELECT new mypackage.CategorySalary( " +
    "    emp.category, " +
    "    emp.salary as somevalue, " +
    "    0 as dummy " +
    ") from ...  " +
    "WHERE ...  ";

TypedQuery<CustomEmployee> typQuery = entityManager.createQuery(query, CustomEmployee.class);
String query=“选择新建mypackage.CategorySalary(”+
“emp类别,”+
“emp.salary作为某个值,”+
“0作为虚拟对象”+
)来自……”+
“哪里……”;
TypedQuery typQuery=entityManager.createQuery(查询,CustomEmployee.class);

谢谢。成功了。不过我还有一个问题。将字符串硬编码为“dummy”作为defaultColumn是可行的,但是当我使用字符串变量作为dummy时+dummy+“AS defaultColumn”它抛出异常NameGenerator.generateColumnNames.Column名称应该与bean的属性名称相对应。
String query = "SELECT new mypackage.CategorySalary( " +
    "    emp.category, " +
    "    emp.salary as somevalue, " +
    "    0 as dummy " +
    ") from ...  " +
    "WHERE ...  ";

TypedQuery<CustomEmployee> typQuery = entityManager.createQuery(query, CustomEmployee.class);