JAVA 使用jpa原生sql查询字段 别名无值,小白求大神慷慨赐教

sql查询语句 ,其中 realPrice 是别名

 @Query(value = "select *,case when db.order_type_code = 'Db2bOrder' then d.RealTotal else  df.TotalMoney end realPrice from db2border_bill_detail db " +
            "left join db2border d on d.fID =db.source_id " +
            "left join db2brefund df on df.OrderID = db.source_id  where db.group_id = ?1 and db.db2border_bill_id = ?2", nativeQuery = true)
    List<Db2BorderBillDetail> findAllByGroupIdAndDb2BorderBillId(String groupId,Long Id);

在Db2BorderBillDetail实体类增加对应字段,这样查询正常 ,返回的realPrice的字段有值 ,但是发现保存报错 提示“Unknown column 'db2borderb0_.realPrice' in 'field list'”

    @Transient()
    private BigDecimal realPrice;

    public BigDecimal getRealPrice() {
        return realPrice;
    }

    public void setRealPrice(BigDecimal realPrice) {
        this.realPrice = realPrice;
    }

改成这样 则保存正常,但是sql查询的值就为null

    private BigDecimal realPrice;

    @Transient()
    public BigDecimal getRealPrice() {
        return realPrice;
    }

    public void setRealPrice(BigDecimal realPrice) {
        this.realPrice = realPrice;
    }

求大神赐教下,怎么才能查询保存都正常

最后还是弄多了一步操作,  查询sql 先返回List<Object[]>   再把List<Object[]>转为 List<Db2BorderBillDetail> 了

@Transient注解表示这个字段不存数据库,加上当然数据库是null。是不是你数据库这个字段命名不规范,realPrice默认会找数据库的real_price。
_如果不是这样写的可以用@column注解设置一下对应关系

case when db.order_type_code = 'Db2bOrder' then d.RealTotal else df.TotalMoney end realPrice
看看你这个判断,你的这个realPrice是从两个表里挑选的字段进行别名,那为什么只在一个表里进行@Transient注解呢?

字段用@TableField(exist=false)注解,即:@Transient()改成@TableField(exist=false)试试,

@TableField(exist=false)
 private BigDecimal realPrice;