java - JPQL参数类型错误

标签 java hibernate jpa

在我当前的项目中,我使用 hibernate JPA 作为持久性库。在为查询设置参数时,我遇到了奇怪的错误。

我有以下内容:

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: Parameter value [43.239474] was not matching type [java.lang.Integer]]

但是由于精度损失,该值无法转换为整数。

有什么建议吗?

最佳答案

如果您在 JPA 下使用 Hibernate,则可以直接使用 Hibernate session 来解决此问题。我遇到了同样的问题,找不到其他方法。

以下应该有效:

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

关于java - JPQL参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15254573/

相关文章:

java - Spring MVC 将用户添加到关注者表按钮不将用户保存到数据库

java - 使用 hibernate/jpa/memcache 缓存大数据集?

java - 使用 Java 将数组输入组织到表中?

java - 在Hibernate中对于composite-id,为什么类应该实现Serialized

java - 如何解决==> org.hibernate.exception.GenericJDBCException : Could not open connection

java - hibernate (4.2.7) : Understanding of CascadeType PERSIST

java - 在字符串模式下转换 3 个不同的 int 值,并用空格或其他内容分隔

java - Android 谷歌地图 - 如何检查折线是否为空

Java : Bound mismatch: is not a valid substitute for the bounded parameter <E extends Comparable<E>>

java - 添加用户名到审计表