gpt4 book ai didi

java - 使用 SqlResultSetMapping 和 ConstructorResult 的类转换异常

转载 作者:行者123 更新时间:2023-12-01 08:47:04 24 4
gpt4 key购买 nike

我正在尝试使用SqlResultSetMapping将NativeQuery的结果映射到非实体pojo。和 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 有exact same example in the javadoc ,那么我做错了什么?

最佳答案

找到了两种解决方案,在这两种情况下,StreetCity 必须是 @Entity

使用@ConstructorResult,它还需要构造函数。

解决方案1,使用@EntityResult而不是@ConstructorResult

@Data
@Entity
@SqlResultSetMapping(name = "StreetCityMapping", entities = @EntityResult(entityClass = StreetCity.class, fields = {
@FieldResult(name = "street", column = "street"), @FieldResult(name = "city", column = "city") }))
public class StreetCity {
@Id
public String street;
public String city;
}

解决方案 2,使用 @ConstructorResult 和构造函数:

@Data
@Entity
@SqlResultSetMapping(name = "StreetCityMapping", classes = {
@ConstructorResult(targetClass =
StreetCity.class, columns = {
@ColumnResult(name = "street", type = String.class),
@ColumnResult(name = "city", type = String.class) }) })
public class StreetCity {
@Id
public String street;
public String city;

public StreetCity() {
}

public StreetCity(String street, String city) {
this.street = street;
this.city = city;
}
}

无需更改其他代码,我猜解决方案 1 是最干净的

关于java - 使用 SqlResultSetMapping 和 ConstructorResult 的类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42610978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com