慕碼人2483693
2023-01-05 15:32:18
我正在使用帶有 Spirng JPA 的 spring boot strater 2.2.0.BUILD-SNAPSHOT。我需要將一些復(fù)雜的 SQL 與一些組一起使用我正在使用基于接口的投影的本機(jī)查詢,我能夠檢索所有字段但返回類型異常java.sql.Timestamp示例:我的實(shí)體類@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;}存儲(chǔ)庫(kù)接口與投影接口@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貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
只需我們使用別名來(lái)匹配您界面中的名稱。不要使用 _ 但使用 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)
原因:使用本機(jī)查詢和接口投影時(shí),未考慮實(shí)體中的列映射。
還要確保你有正確的返回類型接口!
- 1 回答
- 0 關(guān)注
- 220 瀏覽
添加回答
舉報(bào)
0/150
提交
取消