2013-10-07 71 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 
}