我正在尝试将 NativeQuery 的结果映射到非实体 pojo,使用SqlResultSetMapping
和ConstructorResult
我正在使用这个StreetCity
类,带有结果映射:
@Data
@SqlResultSetMapping(name = "StreetCityResult", classes = {
@ConstructorResult(targetClass = StreetCity.class, columns = {
@ColumnResult(name = "street", type = String.class),
@ColumnResult(name = "city", type = String.class) }) })
public class StreetCity {
public String street;
public String city;
public StreetCity(String street, String city) {
this.street = street;
this.city = city;
}
}
这是执行 NativeQuery 的函数
public List<StreetCity> retrieveStreetCity(String zipCode, int houseNumber) {
Query query = em.createNativeQuery(getLocationQuery(zipCode, houseNumber), "StreetCityResult");
List<StreetCity> streetCityList = query.getResultList();
return streetCityList;
}
这是应该执行的查询
public String getLocationQuery(String zipCode, int houseNumber) {
String query =
"select straat.straatnaam as street, plaats.plaatsnaam as city "
+ " from pcreeks "
+ " left join plaats on plaats.plaatsid = pcreeks.plaatsid "
+ " left join straat on straat.straatid = pcreeks.straatid "
+ " where (...) ";
return query;
}
但我仍然遇到异常
2017-03-05T16:35:57.736+0100|Warning: javax.ejb.EJBException
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to nl.xxx.service.business.locations.entity.StreetCity
编辑:错误来自此代码的第二行:
List<StreetCity> streetCityList = storage.retrieveStreetCity(zipCode, houseNumber);
streetCityList.get(0).getStreet();
Oracle在 javadoc 中有完全相同的示例,那么我做错了什么?