如何在EntityManager createNativeQuery中使用结果类?

时间:2017-10-24 18:40:20

标签: 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)
 * @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;

import 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

0 个答案:

没有答案