【发布时间】:2011-03-24 20:06:43
【问题描述】:
我正在使用 HQL 在 Java 中运行查询并创建如下所示的参数:
query.setParameter("month", month);
月份变量是介于 1 和 12 之间的 BigDecimal。但是,月份变量作为对象传递给运行此查询的函数。因此,当它被设置为参数时,它被视为一个对象。
当我运行查询时,我得到了这个错误:
15:46:48,878 INFO [IntegerType] could not bind value '4' to parameter: 1; java.math.BigDecimal
15:46:48,878 ERROR [STDERR] java.lang.ClassCastException: java.math.BigDecimal
15:46:48,878 ERROR [STDERR] at org.hibernate.type.IntegerType.set(IntegerType.java:41)
15:46:48,878 ERROR [STDERR] at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
15:46:48,879 ERROR [STDERR] at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
15:46:48,879 ERROR [STDERR] at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
“4”是存储在我的月份变量中的值。因此,该参数显然有问题。这可能是什么原因造成的?
【问题讨论】:
-
您使用的是 HQL 还是 JPQL(例如 Hibernate 或 JPA)?
-
查询完全用HQL编写。
-
你为什么要使用
BigDecimal来存储 1 到 12 之间的值? -
因为获取月份和年份作为参数传递的函数返回 BigDecimals。我所做的所有其他查询都做同样的事情。但是由于某种原因,这个不起作用。