我正在使用带有 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()