0

我正在尝试将 NativeQuery 的结果映射到非实体 pojo,使用SqlResultSetMappingConstructorResult

我正在使用这个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 中有完全相同的示例,那么我做错了什么?

4

1 回答 1

1

找到了两个解决方案,在这两种情况下,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 是最干净的

于 2017-03-06T09:41:56.773 回答