简体   繁体   English

JPQL参数类型错误

[英]JPQL parameter type error

In my current project I'm using hibernate JPA as a persistence library. 在我当前的项目中,我正在使用休眠JPA作为持久性库。 And I'm getting strange error when setting the parameters to the query. 并将参数设置为查询时出现奇怪的错误。

I have the following: 我有以下内容:

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();

The result error is: 结果错误是:

[IllegalArgumentException: Parameter value [43.239474] was not matching type [java.lang.Integer]] [IllegalArgumentException:参数值[43.239474]与类型[java.lang.Integer]不匹配]

But this value cannot be cast to integer because of loss of precision. 但是由于精度损失,该值不能转换为整数。

Any suggestions? 有什么建议么?

If you're using Hibernate under JPA, you can resolve this problem by using the Hibernate session directly. 如果您在JPA下使用Hibernate,则可以直接使用Hibernate会话来解决此问题。 I had the same problem and couldn't find another way. 我遇到了同样的问题,找不到其他方法。

The following should work: 以下应该工作:

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();

JPA expects a Double type parameter value for :radius. JPA需要:radius的Double类型参数值。 So just update the radius parameter assignment as ; 因此,只需将半径参数分配更新为;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM