我有一些本地查詢並希望查詢執行的結果映射到非實體POJO類的列表:如何使用@ConstructorResult註釋
@SqlResultSetMapping(
name = "SomeMapping",
classes = {
@ConstructorResult(targetClass = SomeClass.class,
columns = {
@ColumnResult(name = "id", type = Integer.class),
@ColumnResult(name = "NAME", type = String.class),
@ColumnResult(name = "DATE_BEGIN", type = java.util.Date.class)
}
)
}
)
public class SomeClass{
private Integer id;
private String name;
private java.util.Date begDate;
private java.util.Date endDate;
public SomeClass(Integer id, String name, Date begDate){
this.id = id;
this.name = name;
this.begDate = begDate;
}
//Getters & Setters ...
}
一般我如何想檢索查詢執行的結果:
String query = "SELECT " +
"A.id AS 'id', " +
"A.name AS 'NAME', " +
"A.begDate AS 'DATE_BEGIN' " +
"FROM " +
"SomeTable A " +
"WHERE A.endDate < '2014-01-01'";
List<SomeClass> result = em.createNativeQuery(query, "SomeMapping").getResultList();
但我總是得到對象列表。如何檢索我的POJO-s的列表?
這是問題的一個很好的解釋 https://stackoverflow.com/questions/24160817/getting-error-could-not-locate-appropriate-constructor-on-class?answertab=votes#tab-top –