2

Looking at EntityManager's interface I see there is a signature to create a native query with a result class.

/**
 * 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);

After much research I am still unclear on how to use the resultClass param. Typically my resultsets from native queries are not the same as my entities; they are usually composites/derived results from multiple functions/joins/etc. So is there a way I could make a class like this

public class MyCustomResultRow {
private String propa;
private String propb;
private UUID id;
...
}

Then run code like this?

List<MyCustomResultRow> results = createNativeQuery("some query", MyCustomResultRow.class).getResultList();

Do I have to annotate MyCustomResultRow? Do I have to add it to my persistence.xml file somehow? Any ideas?

If I just run createNativeQuery(strSql, CodeSupportAndCapacityResult.class); where CodeSupportAndCapacityResult looks like this:

package com.wastecenter.portal.backend.admin;

import java.time.Instant;

public class 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;
}

}

Then I get this.

javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.wastecenter.portal.backend.admin.CodeSupportAndCapacityResult
benstpierre
  • 32,833
  • 51
  • 177
  • 288
  • I believe this should work as long as the JPA provider can create instances of `MyCustomResultRow` and can map the columns of the SQL result to properties of the `MyCustomResultRow` bean. If the mapping is not straightforward, you can define custom mappings with `@SqlResultSetMapping`. What exactly is not working for you? – Nikos Paraskevopoulos Oct 24 '17 at 18:56
  • As per my edit above I get javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.wastecenter.portal.backend.admin.CodeSupportAndCapacityResult – benstpierre Oct 24 '17 at 19:16

0 Answers0