2013-10-07 72 views
1

我有以下JPQL查询:在JPQL使用COUNT查询

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)映射到我的DestinationInfo DTO。

这是我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 
} 
+0

@AndreiI是的,我是有作为'int'最初实际上,然后将其改为'Integer',看看是否会工作 – Mekswoll

回答

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 
}