簡體   English   中英

@Query 結果到 DTO 導致 spring 數據 jpa 中的構造函數錯誤

[英]@Query result to DTO causes constructor error in spring data jpa

我的 JpaRepository 中有如下查詢

@Query("select new com.x.airlinesystem.dto.ticket.TicketPriceDTO((select count(t) from Ticket t where t.flight=:flightId and t.ticketStatus=:ticketStatus),f.capacity,f.price) from Flight f where f.id = :flightId")
TicketPriceDTO findPriceInfo(@Param("flightId") Long flightId,
        @Param("ticketStatus") TicketStatus ticketStatus);

我的 dto 如下所示;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TicketPriceDTO implements Serializable {

    private Integer sold;
    private Integer capacity;
    private BigDecimal price;
}

最后我的 TicketStatus 枚舉是;

public enum TicketStatus {
    SOLD, RETURNED
}

當我編譯項目時,出現如下錯誤;

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.x.airlinesystem.dto.ticket.TicketPriceDTO]. Expected arguments are: long, int, java.math.BigDecimal [select new com.x.airlinesystem.dto.ticket.TicketPriceDTO((select count(t) from com.x.airlinesystem.entity.Ticket t where t.flight= :flightId 和 t.ticketStatus=:ticketStatus),f.capacity,f.price) 來自 com.x.airlinesystem.entity.Flight f 其中 f.id =:flightId]

只是預感,但確實

(select count(t) from Ticket t where t.flight=:flightId and t.ticketStatus=:ticketStatus

回來多久?

也許改變private Integer sold; private Long sold; ?

// Hi you can try to change Integer with Long

private Integer sold;
private Integer capacity;
private BigDecimal price;

//to replace with 

private Long sold;
private Integer capacity;
private BigDecimal price;

//I can see Expected arguments are: long, int, java.math.BigDecimal is looking for long type

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM