为了账号安全,请及时绑定邮箱和手机立即绑定

如果返回类型为 java.sql.Timestamp,则基于接口的投影 getter 方法给出异常

如果返回类型为 java.sql.Timestamp,则基于接口的投影 getter 方法给出异常

Go
慕码人2483693 2023-01-05 15:32:18

我正在使用带有 Spirng JPA 的 spring boot strater 2.2.0.BUILD-SNAPSHOT。我需要将一些复杂的 SQL 与一些组一起使用我正在使用基于接口的投影的本机查询,我能够检索所有字段但返回类型异常java.sql.Timestamp


示例:我的实体类


@Entity

@Table(name = "trade")

public class Trade {


@Id

@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name = "trade_id")

private long tradeId;


@Column(name = "symbol")

private String symbol;


@Column(name = "exchange")

private String exchange;


@Column(name = "segment")

private String segment;


@Column(name = "clinet_trade_id")

private long clientTradeId;


@Column(name = "order_id")

private long orderId;


@Column(name = "price")

private double price;


@Column(name = "sell_type")

private String tradeType;


@Column(name = "quantity")

private int quantity;


@Column(name = "trade_date_time")

private Timestamp tradeDateTime;

}

存储库接口与投影接口


@Repository("TradeRepository")

public interface TradeRepository extends JpaRepository<Trade, Long> {



// projection


//https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

//Interface-based Projections

//https://www.baeldung.com/spring-data-jpa-projections


@Query(value = "select symbol,date(trade_date_time) as trade_date_time , sum(quantity) as quantity , sum(quantity*price) as price  from trade where sell_type ='sell' \n" + 

        "and trader_date > ?1  and trader_date < ?2 and user_id= ?3 " + 

        "group by symbol, date(trade_date_time) " + 

        "order by symbol, date(trade_date_time) ", nativeQuery = true)

 public List<TradeView> findByNativeQuery(Timestamp fromdate, Timestamp toDate, int userId);


public static interface TradeView {

     public String getSymbol();

     public Timestamp getTradeDateTime();

     public int getQuantity();

     public double getPrice();

  }



}

在这里我得到该方法的异常getTradeDateTime()


查看完整描述

1 回答

?
蝴蝶刀刀

TA贡献1566条经验 获得超8个赞

只需我们使用别名来匹配您界面中的名称。不要使用 _ 但使用 tradeDateTime:


@Query(value = "select symbol, date(trade_date_time) as tradeDateTime, sum(quantity) as quantity , sum(quantity*price) as price  from trade where sell_type ='sell' \n" + 

    "and trader_date > ?1  and trader_date < ?2 and user_id= ?3 " + 

    "group by symbol, date(trade_date_time) " + 

    "order by symbol, date(trade_date_time) ", nativeQuery = true)

原因:使用本机查询和接口投影时,未考虑实体中的列映射。


还要确保你有正确的返回类型接口!


查看完整回答
反对 回复 2023-01-05
  • 1 回答
  • 0 关注
  • 16 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信