在我当前的项目中,我使用 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/