2013-03-06 105 views
1

在我當前的項目中,我使用hibernate JPA作爲持久性庫。當設置參數爲查詢時,我得到奇怪的錯誤。JPQL參數類型錯誤

我有以下:

List<Location> loca = JPA.em().createQuery(
    "SELECT location FROM Location as location" 
    + " where " 
    + "(6371 * acos(cos(PI()/180 * :platitude) * cos(PI()/180 *location.latitude ) * " 
    + "cos(PI()/180 *location.longitude - PI()/180 *:plongitude) + sin(PI()/180 *:platitude) * " 
    + "sin(PI()/180 *location.latitude))) < :radius") 
    .setParameter("radius", 10000.0) 
    .setParameter("platitude", new Double(latitude)) 
    .setParameter("plongitude", new Double(longitude)) 
    .getResultList(); 

結果錯誤是:

[拋出:IllegalArgumentException:參數值[43.239474]沒有匹配類型[java.lang.Integer中]]

但是由於精度損失,此值無法轉換爲整數。

有什麼建議嗎?

+0

您使用的數據庫是?數據庫中的字段類型是十進制的嗎?您設置的實體屬性是否爲雙精度? – rbento 2013-03-06 18:26:05

+0

我使用postgresql,是的實體屬性是雙重的,而數據庫字段是「雙精度」。 – 2013-03-06 18:58:43

+1

您是否嘗試用180.0替換180? – 2013-03-06 20:23:51

回答

1

如果您在JPA下使用Hibernate,則可以通過直接使用Hibernate會話來解決此問題。我有同樣的問題,找不到另一種方式。

下面應該工作:

Session session = (Session) JPA.em().getDelegate(); 
List<Location> loca = session.createQuery(
    "SELECT location FROM Location as location" 
    + " where " 
    + "(6371 * acos(cos(PI()/180 * :platitude) * cos(PI()/180 *location.latitude ) * " 
    + "cos(PI()/180 *location.longitude - PI()/180 *:plongitude) + sin(PI()/180 *:platitude) * " 
    + "sin(PI()/180 *location.latitude))) < :radius") 
    .setParameter("radius", 10000.0, new DoubleType()) 
    .setParameter("platitude", new Double(latitude), new DoubleType()) 
    .setParameter("plongitude", new Double(longitude), new DoubleType()) 
    .list(); 
0

JPA預計,Double類型參數值:半徑。所以只需更新半徑參數分配爲;

.setParameter("radius", new Double(10000.0)) 
+0

導致問題的值是43.239474 – JamesB 2015-08-25 09:05:56