Spring数据抛出异常:“找不到类型[Object]的属性[property_name]!”

时间:2016-02-29 15:48:45

标签: java spring jpa spring-data

我想要一个Spring Data接口来返回一个非实体对象。

我用google搜索并找到了一个解决方案,我应该使用命名查询,并将结果集映射到非实体类。

但是,我确实解除了上述异常。造成这种异常的原因是什么?

这是我对sql结果集映射和命名本机查询的实现:

@SqlResultSetMapping(
        name = "revenue",
        classes = {
                @ConstructorResult(
                        targetClass = Revenue.class,
                        columns = {
                                @ColumnResult(name = "date"),
                                @ColumnResult(name = "revenue")
                        }
                )
        }
)
@NamedNativeQuery(name = "calculateRevenueForGasStationName",
        query = "select fueling.date, fueling.price*fueling.volume as revenue from gas_station " +
                "left join fueling " +
                "on gas_station.id = fueling.gas_station " +
                "where fueling.date between ?1 and ?2 and gas_station.name=?3 " +
                "group by fueling.date",
        resultSetMapping = "revenue")

这是应该链接到name = "calculateRevenueForGasStationName"

的spring数据接口方法
@Query(nativeQuery = true)
List<Revenue> calculateRevenueForGasStationName(Date from, Date to, String name);

1 个答案:

答案 0 :(得分:0)

您的类属性似乎有问题。您能否请重新访问您的Revenue.class属性详细信息。

请确认 1)“date”属性声明为java.util.Date 2)收入已宣布为Double 3)示例代码是

`@SqlResultSetMapping( 
name = "revenue", 
classes = { @ConstructorResult( 
targetClass = Revenue.class, 
columns = { 
@ColumnResult(name = "date", type = java.util.Date.class), 
@ColumnResult(name = "revenue",type = Double.class) 
} ) } )`