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']
我的代码哪里出了问题?有什么想法吗?
createQuery(String, Class)
而不是createNativeQuery(String, Class)
吗?我认为 JPA 期望TableA
是一个模型类,而不是将其用作表名。 - user520458