Spring Data JPA 查询结果返回自定义实体

1、自定义实体

/**

  • Created by administor on 2018-7-18 17:50.
    */
    public class CarTraceResult {
    private String plateNo;
    private Integer plateColor;
    private String typeName;
    private String parkName;
    private Calendar time;
    private Long times;

    public CarTraceResult(String plateNo, Integer plateColor, String typeName, String parkName, Calendar time, Long times) {
    this.plateNo = plateNo;
    this.plateColor = plateColor;
    this.typeName = typeName;
    this.parkName = parkName;
    this.time = time;
    this.times = times;
    }

    //省略get、set方法
    }
    CarTraceResult需要实现构造方法,因为后面会用到。
    2、查询

    @Query(value = "select new com.park.carTrace.pojo.CarTraceResult(a.plateNo, a.plateColor, a.typeName, a.parkName, max(a.time) as time, count(a.id) as times) " +
    " from CarTraceRecordEntity a where a.plateNo = ?1 and a.plateColor = ?2 " +
    "and a.type = ?3 group by a.parkNo order by time desc ")
    List queryCarTraceRecord(String plateNo, Integer plateColor, Integer type);
    网友有强调自定义实体类的属性和名称要与查询结果完全对应,那么问题来了,由于我使用了max()和count()函数,并分别将对应结果起了别名,该语句在执行中是报错的。到这里,似乎是无解了。反复尝试后,大胆将代码改为:

@Query(value = "select new com.park.carTrace.pojo.CarTraceResult(a.plateNo, a.plateColor, a.typeName, a.parkName, max(a.time), count(a.id)) " +
" from CarTraceRecordEntity a where a.plateNo = ?1 and a.plateColor = ?2 " +
"and a.type = ?3 group by a.parkNo order by time desc ")
List queryCarTraceRecord(String plateNo, Integer plateColor, Integer type);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值