1

我有以下 JPQL 查询:

    List<DestinationInfo> destinations = em.createQuery("SELECT NEW com.realdolmen.patuva.dto.DestinationInfo(d.name, d.continent, MIN(t.departureDate), MIN(t.pricePerDay), COUNT(t.id))" +
            " FROM Destination d, Trip t" +
            " WHERE d.continent = :continent " +
            " GROUP BY d.name, d.continent").setParameter("continent", searchedContinent).getResultList();

如果我运行它,我会收到错误:

javax.ejb.EJBTransactionRolledbackException: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.realdolmen.patuva.dto.DestinationsList]

如果我省略并从构造函数COUNT(t.id)中删除该参数,它可以正常工作。DestinationInfo为什么我不能将 映射COUNT(t.id)到我的DestinationInfoDTO。

这是我的DestinationInfo课:

public class DestinationInfo {
    private String name;
    private Continent continent;
    private Date earliestDeparture;
    private Integer totalNumTrips;
    private BigDecimal lowestPrice;

    public DestinationInfo(String name, Continent continent, Date earliestDeparture, BigDecimal lowestPrice, Integer totalNumTrips) {
        this.name = name;
        this.continent = continent;
        this.earliestDeparture = earliestDeparture;
        this.totalNumTrips = totalNumTrips;
        this.lowestPrice = lowestPrice;
    }

    // getters and setters
}
4

1 回答 1

2

显然COUNT(t.id)返回了一些类型long。将类更改DestinationInfo为以下使其工作:

public class DestinationInfo {
    private String name;
    private Continent continent;
    private Date earliestDeparture;
    private long totalNumTrips;
    private BigDecimal lowestPrice;

    public DestinationInfo(String name, Continent continent, Date earliestDeparture, BigDecimal lowestPrice, long totalNumTrips) {
        this.name = name;
        this.continent = continent;
        this.earliestDeparture = earliestDeparture;
        this.totalNumTrips = totalNumTrips;
        this.lowestPrice = lowestPrice;
    }

    // getters and setters
}
于 2013-10-07T14:12:54.063 回答