Hibernate 如何在EntityManager createNativeQuery中使用resultclass?
查看EntityManager的接口,我看到有一个用于创建带有结果类的本机查询的签名Hibernate 如何在EntityManager createNativeQuery中使用resultclass?,hibernate,jpa,nativequery,hibernate-native-query,Hibernate,Jpa,Nativequery,Hibernate Native Query,查看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) *
/**
* 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();
导入java.time.Instant
公共类代码SupportandCapacityResult{
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
我认为,只要JPA提供者能够创建
MyCustomResultRow
的实例,并且能够将SQL结果的列映射到MyCustomResultRow
bean的属性,这应该是可行的。如果映射不直接,可以使用@SqlResultSetMapping
定义自定义映射。到底什么对您不起作用?根据我上面的编辑,我得到javax.persistence.PersistenceException:org.hibernate.MappingException:Unknown entity:com.wastecenter.portal.backend.admin.CodeSupportAndCapacityResult
javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.wastecenter.portal.backend.admin.CodeSupportAndCapacityResult