JPQL构造函数表达式 - org.hibernate.hql.ast.QuerySyntaxException:未映射表

4
我的初始问题是 https://stackoverflow.com/questions/12172614/hql-join-without-foreign-key-reference,但在这方面找不到任何解决方案,因此使用 JPA 中的本地查询向前移动。 entityManager 的 createNativeQuery 返回 Query 对象,该对象又返回 List<Object[]>。我不想在迭代列表时处理索引,因为它具有错误倾向性。因此,我寻找了其他解决方案,并发现 JPQL 的构造函数表达式是其中之一。

表结构是:

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运行此查询时

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']

我的代码哪里出了问题?有什么想法吗?

你应该使用本地查询和结果集映射器。 - Amir Pashazadeh
1
你难道不应该使用 createQuery(String, Class) 而不是 createNativeQuery(String, Class) 吗?我认为 JPA 期望 TableA 是一个模型类,而不是将其用作表名。 - user520458
@arturo,如果我使用createNativeQuery(String,class)方法,会得到一个异常org.hibernate.MappingException:Unknown entity:com.xyz.PersonSearch。我认为,在这种情况下,它也期望将其作为实体。我找不到关于此用法的适当文档。 - Pankaj
谁说你应该使用createNativeQuery?他是在说你的JPQL查询是“错误”的(这在JPA规范和任何体面的JPA实现的文档中都有很好地定义)。 - DataNucleus
2个回答

9
根据书籍“Pro EJB 3 Java Persistence API”,构造函数表达式。

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

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.

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对象列表。
我相信你的SQL中的“.... From Schema1.TableA ms ...”应该引用一个被映射的实体。因此,你应该有一个映射到TableA的实体,然后JPQL应该更接近于“.... From MyTableAEntity ms ...”,其中MyTableAEntity具有将其映射到数据库表TableA的所有适当的JPA注释。正如书籍代码片段所述,“SELECT NEW ...”的目标不一定要被映射,但是必须在FROM子句中引用实体。

0

要实现这一点,您可以在JPA查询中使用JPQL(Java持久性查询语言)构造器表达式。构造器表达式用于创建自定义结果类的实例。

  1. 在自定义类中创建构造函数:

首先,创建一个自定义类来保存结果信息。在您的情况下,这个类可能被命名为BookAuthorInfo,并且有一个接受必要值(作者姓名和书籍标题)的构造函数。

public class BookAuthorInfo {
private String name;
private String title;

public BookAuthorInfo(String name, String title) {
    this.name = name;
    this.title = title;
}

}

写构造函数查询:
接下来,在您的JPQL查询中使用构造函数表达式,从实体中选择所需的属性并构造自定义类的实例。
TypedQuery<BookAuthorInfo> query = entityManager.createQuery(
"SELECT NEW com.example.BookAuthorInfo(a.name, b.title) " +
"FROM Book b JOIN b.author a " +
"WHERE b.publicationYear = :year", BookAuthorInfo.class);

query.setParameter("year", desiredYear); List results = query.getResultList();

  • NEW com.example.BookAuthorInfo(a.name, b.title) 使用提供的构造函数创建 BookAuthorInfo 类的实例,并从 Author 和 Book 实体中传递选定的属性。
  • JOIN b.author a 指定了 Book 和 Author 实体之间的关系。
  • WHERE b.publicationYear = :year 根据期望的出版年份过滤图书。

这种方法允许您从多个实体中获取特定属性,并使用基于构造函数的 JPA 查询构造自定义结果对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接