问题描述
我原来的问题是
,但找不到任何解决方案,因此使用JPA进行原生查询。 entityManager的createNativeQuery返回Query对象,然后返回 List< Object []> 。我不想在迭代列表时处理索引,因为它本质上是错误的。因此,我查看了一些其他解决方案,并发现JPQL的构造函数表达式是解决方案之一。
表结构是
$ $ p $ code> Schema1 -TableA
- NameColumn
- PhoneColumn
对应的Java类是
public class PersonSearch implements Serializable {
public PersonSearch(String NameColumn,String PhoneColumn){
this.name = NameColumn;
this.phone = PhoneColumn;
}
私人字符串名称;
私人字符串电话;
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;
$ / code $ / pre
查询是
选择新的com.xyz.PersonSearch(ms.NameColumn,ms.PhoneColumn)从Schema1.TableA ms其中ms.PhoneColumn ='9134409930'
使用entityManager API运行此查询
entityManager.createQuery(queryString,PersonSearch.class);
得到低于错误值。
原因:org.hibernate.hql.ast.QuerySyntaxException:Schema1.TableA未映射[Select new com.xyz.PersonSearch(ms.NameColumn,ms.PhoneColumn)From Schema1.TableA ms Where ms .PHONE ='9134409930']
我的代码有什么问题?任何想法?根据书Pro EJB 3 Java Persistence API
$ b 解释方案 $ b构造函数表达式
$ b示例代码如下
列表结果= 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的一部分.... Schema1.TableA ms ..应该指向映射的实体。所以你应该有一个映射到TableA的实体,然后jpql应该更多地沿着...... MyTableAEntity ms ...的行,其中MyTableAEntity具有将其映射到DB表TableA的所有适当jpa注释。正如本书的片段所述,SELECT NEW ...的目标不必被映射,但FROM子句中引用的实体不需要。
My original problem was https://stackoverflow.com/questions/12172614/hql-join-without-foreign-key-referencebut 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.
Table structure is
Schema1 -TableA - NameColumn - PhoneColumnCorresponding Java class is
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; } }Query is
Select NEW com.xyz.PersonSearch(ms.NameColumn, ms.PhoneColumn) From Schema1.TableA ms Where ms.PhoneColumn='9134409930'while running this query using entityManager API
entityManager.createQuery(queryString, PersonSearch.class);getting below error.
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 ?
解决方案according to the book "Pro EJB 3 Java Persistence API"
Constructor Expressions
The example code is as follows
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();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.
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:表未映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!