@query result to dto导致spring数据jpa中的构造函数错误

zzoitvuj  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(318)

我在我的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;
}

最后我的票证状态是;

public enum TicketStatus {
    SOLD, RETURNED
}

当我编译这个项目时,我得到如下错误;
原因:java.lang.illegalargumentexception:org.hibernate.hql.internal.ast.querysyntaxexception:无法在类[com.x.airlinesystem.dto.ticket.ticketpricedto]上找到相应的构造函数。预期参数为: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 and t.ticketstatus=:ticketstatus),f.capacity,f.price)from com.x.airlinesystem.entity.flightf where f.id=:flightid]

vbopmzt1

vbopmzt11#

只是一种预感,但确实如此 (select count(t) from Ticket t where t.flight=:flightId and t.ticketStatus=:ticketStatus 回来很久了?
也许改变就足够了 private Integer sold;private Long sold; ?

taor4pac

taor4pac2#

// 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

相关问题