查看 EntityManager 的界面,我看到有一个签名可以使用结果类创建本机查询。
/**
* Create an instance of <code>Query</code> for executing
* a native SQL query.
* @param sqlString a native SQL query string
* @param resultClass the class of the resulting instance(s)
* @return the new query instance
*/
public Query createNativeQuery(String sqlString, Class resultClass);
经过大量研究,我仍然不清楚如何使用 resultClass 参数。通常,我的本地查询结果集与我的实体不同;它们通常是来自多个函数/连接/等的复合/派生结果。那么有没有办法让我像这样上课
public class MyCustomResultRow {
private String propa;
private String propb;
private UUID id;
...
}
然后像这样运行代码?
List<MyCustomResultRow> results = createNativeQuery("some query", MyCustomResultRow.class).getResultList();
我必须注释 MyCustomResultRow 吗?我是否必须以某种方式将其添加到我的 persistence.xml 文件中?有任何想法吗?
如果我只是运行 createNativeQuery(strSql, CodeSupportAndCapacityResult.class); CodeSupportAndCapacityResult 看起来像这样:
package com.wastecenter.portal.backend.admin;
导入java.time.Instant;
公共类 CodeSupportAndCapacityResult {
private String waste_disposal_facility_id;
private String waste_code;
private Long latest_support_entry_id;
private Instant latest_support_entry_created;
private String latest_support_entry_comment;
private Long latest_capacity_entry_id;
private String latest_capacity_entry_type;
private String latest_capacity_entry_comment;
private Instant latest_capacity_entry_created;
public String getWaste_disposal_facility_id() {
return waste_disposal_facility_id;
}
public void setWaste_disposal_facility_id(String waste_disposal_facility_id) {
this.waste_disposal_facility_id = waste_disposal_facility_id;
}
public String getWaste_code() {
return waste_code;
}
public void setWaste_code(String waste_code) {
this.waste_code = waste_code;
}
public Long getLatest_support_entry_id() {
return latest_support_entry_id;
}
public void setLatest_support_entry_id(Long latest_support_entry_id) {
this.latest_support_entry_id = latest_support_entry_id;
}
public Instant getLatest_support_entry_created() {
return latest_support_entry_created;
}
public void setLatest_support_entry_created(Instant latest_support_entry_created) {
this.latest_support_entry_created = latest_support_entry_created;
}
public String getLatest_support_entry_comment() {
return latest_support_entry_comment;
}
public void setLatest_support_entry_comment(String latest_support_entry_comment) {
this.latest_support_entry_comment = latest_support_entry_comment;
}
public Long getLatest_capacity_entry_id() {
return latest_capacity_entry_id;
}
public void setLatest_capacity_entry_id(Long latest_capacity_entry_id) {
this.latest_capacity_entry_id = latest_capacity_entry_id;
}
public String getLatest_capacity_entry_type() {
return latest_capacity_entry_type;
}
public void setLatest_capacity_entry_type(String latest_capacity_entry_type) {
this.latest_capacity_entry_type = latest_capacity_entry_type;
}
public String getLatest_capacity_entry_comment() {
return latest_capacity_entry_comment;
}
public void setLatest_capacity_entry_comment(String latest_capacity_entry_comment) {
this.latest_capacity_entry_comment = latest_capacity_entry_comment;
}
public Instant getLatest_capacity_entry_created() {
return latest_capacity_entry_created;
}
public void setLatest_capacity_entry_created(Instant latest_capacity_entry_created) {
this.latest_capacity_entry_created = latest_capacity_entry_created;
}
}
然后我明白了。
javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.wastecenter.portal.backend.admin.CodeSupportAndCapacityResult