JPQL 构造函数表达式 - org.hibernate.hql.ast.QuerySyntaxException:表未映射 [英] JPQL Constructor Expression - org.hibernate.hql.ast.QuerySyntaxException:Table is not mapped

查看:32
本文介绍了JPQL 构造函数表达式 - org.hibernate.hql.ast.QuerySyntaxException:表未映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我原来的问题是 https://stackoverflow.com/questions/12172614/hql-join-without-foreign-key-reference但找不到任何解决方案,因此使用 JPA 进行本机查询.entityManager 的 createNativeQuery 返回 Query 对象,该对象又返回 List.我不想在迭代列表时处理索引,因为它本质上容易出错.因此我查看了其他一些解决方案,发现 JPQL 的构造函数表达式是解决方案之一.

My original problem was https://stackoverflow.com/questions/12172614/hql-join-without-foreign-key-reference but couldn't find any solution for this, hence moved forward with native query using JPA. createNativeQuery of entityManager returns Query object which in turn returns List<Object[]>. I don't want to deal with indexes while iterating the list because it's error prone in nature.Therefore i looked at some other solution for it and found JPQL's Constructor expressions as one of the solution.

表结构是

Schema1 -TableA

 - NameColumn
 - PhoneColumn

对应的Java类是

    public class PersonSearch implements Serializable {

    public PersonSearch (String NameColumn, String PhoneColumn) {
        this.name = NameColumn;
        this.phone = PhoneColumn;
    }

    private String name;

    private String phone;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPhone() {
    return phone;
    }

    public void setPhone(String phone) {
    this.phone = phone;
    }
    }

查询是

 Select NEW com.xyz.PersonSearch(ms.NameColumn, ms.PhoneColumn) From Schema1.TableA ms Where ms.PhoneColumn='9134409930'

使用 entityManager API 运行此查询时

while running this query using entityManager API

entityManager.createQuery(queryString, PersonSearch.class);

低于错误.

Caused by: org.hibernate.hql.ast.QuerySyntaxException: Schema1.TableA is not mapped   [Select NEW com.xyz.PersonSearch(ms.NameColumn, ms.PhoneColumn) From Schema1.TableA ms Where ms.PHONE='9134409930']

我的代码有什么问题?有什么想法吗?

What's wrong with my code? Any idea ?

推荐答案

根据《Pro EJB 3 Java Persistence API》一书

according to the book "Pro EJB 3 Java Persistence API"

构造函数表达式

涉及多个表达式的 SELECT 子句的一种更强大的形式是构造函数表达式,它指定查询的结果将使用用户指定的存储对象类型.考虑以下查询:

A more powerful form of SELECT clause involving multiple expressions is the constructor expression, which specifies that the results of the query are to be stored using a user-specified object type. Consider the following query:

SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name)
FROM Employee e

此查询的结果类型是 example.EmployeeDetails 类型.作为查询处理器迭代查询的结果,它使用 EmployeeDetails 实例化新的实例与查询中列出的表达式类型匹配的构造函数.在这种情况下,表达式类型是 String、Double 和 String,因此查询引擎将搜索具有这些类型的构造函数参数的类类型.因此,结果查询集合中的每一行都是EmployeeDetails 包含员工姓名、工资和部门名称.

The result type of this query is the type example.EmployeeDetails. As the query processor iterates over the results of the query, it instantiates new instances of EmployeeDetails using the constructor that matches the expression types listed in the query. In this case the expression types are String, Double, and String, so the query engine will search for a constructor with those class types for arguments. Each row in the resulting query collection is therefore an instance of EmployeeDetails containing the employee name, salary, and department name.

必须使用对象的完全限定名称来引用结果对象类型.这然而,类不必以任何方式映射到数据库.任何带有构造函数的类与 SELECT 子句中列出的表达式兼容,可以在构造函数中使用表达.

The result object type must be referred to using the fully qualified name of the object. The class does not have to be mapped to the database in any way, however. Any class with a constructor compatible with the expressions listed in the SELECT clause can be used in a constructor expression.

构造函数表达式是构建粗粒度数据传输的强大工具用于其他应用程序层的对象或视图对象.而不是手动构建这些对象,单个查询可用于将准备展示的视图对象聚集在一起在网页上.

Constructor expressions are powerful tools for constructing coarse-grained data transfer objects or view objects for use in other application tiers. Instead of manually constructing these objects, a single query can be used to gather together view objects ready for presentation on a web page.

示例代码如下

List result = em.createQuery("SELECT NEW example.EmpMenu(e.name, e.department.name) " +
         "FROM Project p JOIN p.employees e " +
         "WHERE p.name = ?1 " +
        "ORDER BY e.name").setParameter(1, projectName).getResultList();

EmpMenu 类是一个简单的 pojo,没有注释,但有正确的构造函数来匹配构造函数表达式.结果是返回的每一行的 EmpMenu 对象列表.

The EmpMenu class is a simple pojo, no annotations but has the correct constructor to match the constructor expression. The result is a List of EmpMenu objects for each row returned.

我相信您的 SQL 部分.... From Schema1.TableA ms .."应该指的是映射的实体.因此,您应该有一个实体映射到 TableA,然后 jpql 应该更类似于.... From MyTableAEntity ms ...",其中 MyTableAEntity 具有将其映射到 DB 表 TableA 的所有正确的 jpa 注释.正如书籍片段所述,不必映射SELECT NEW ..."的目标,但在 FROM 子句中引用的实体需要映射.

I believe the part of your SQL ".... From Schema1.TableA ms .." should refer to an entity that is mapped. So you should have an entity mapped to TableA, and then the jpql should be something more along the lines of ".... From MyTableAEntity ms ..." where MyTableAEntity has all the proper jpa annotations mapping it to DB table TableA. As the book snippet states, the target of "SELECT NEW ..." does not have to be mapped, but the entity referred to in the FROM clause does.

这篇关于JPQL 构造函数表达式 - org.hibernate.hql.ast.QuerySyntaxException:表未映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆