我正在使用带有 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贡献1801条经验 获得超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)
原因:使用本机查询和接口投影时,未考虑实体中的列映射。
还要确保你有正确的返回类型接口!
- 1 回答
- 0 关注
- 176 浏览
添加回答
举报
0/150
提交
取消