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

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


慕码人2483693
浏览 251回答 1
1回答

蝴蝶刀刀

只需我们使用别名来匹配您界面中的名称。不要使用 _ 但使用 tradeDateTime:@Query(value = "select symbol, date(trade_date_time) as tradeDateTime, sum(quantity) as quantity , sum(quantity*price) as price&nbsp; from trade where sell_type ='sell' \n" +&nbsp;&nbsp; &nbsp; "and trader_date > ?1&nbsp; and trader_date < ?2 and user_id= ?3 " +&nbsp;&nbsp; &nbsp; "group by symbol, date(trade_date_time) " +&nbsp;&nbsp; &nbsp; "order by symbol, date(trade_date_time) ", nativeQuery = true)原因:使用本机查询和接口投影时,未考虑实体中的列映射。还要确保你有正确的返回类型接口!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go