java.lang.ClassCastException:[Ljava.lang.Long;无法转换为 java.lang.Long
java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.lang.Long
我有以下型号 class:
@Entity
@Table(name="user_content")
@org.hibernate.annotations.NamedQueries({
@org.hibernate.annotations.NamedQuery(
name = "checkThatImagesAreModerated",
query = "select contentId from UserContent where contentId in(:imageIdList) and moderationStatus!='TRUE'"
)
})
public class UserContent {
...
和以下 dao:
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("checkThatImagesAreModerated");
query.setParameter("imageIdList", imageIds );
return query.list();
最后一行执行的结果如下所示:
java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.lang.Long
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.terminal.dao.impl.ContentDaoImpl.checkThatImagesAreModerated(ContentDaoImpl.java:105)
at com.terminal.service.impl.ContentServiceImpl.checkThatImagesAreModerated(ContentServiceImpl.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
.....
休眠日志:
Hibernate:
select
userconten0_.content_id as col_0_0_
from
user_content userconten0_
where
(
userconten0_.content_id in (
?
)
)
and userconten0_.moderation_status<>'TRUE'
我不明白 hibernate 尝试转换的位置和原因 Long[] to Long
P.S.
使用query.uniqueResult(),列表方法returns一个结果列表,即使只有一个。内部映射可能会将其解析为数组
尝试 query.setParameterList("imageIdList", imageIds);
而不是 query.setParameter("imageIdList", imageIds);
。当为 in
子句绑定参数时,您通常需要使用它。查看 docs for Query.java
我有以下型号 class:
@Entity
@Table(name="user_content")
@org.hibernate.annotations.NamedQueries({
@org.hibernate.annotations.NamedQuery(
name = "checkThatImagesAreModerated",
query = "select contentId from UserContent where contentId in(:imageIdList) and moderationStatus!='TRUE'"
)
})
public class UserContent {
...
和以下 dao:
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("checkThatImagesAreModerated");
query.setParameter("imageIdList", imageIds );
return query.list();
最后一行执行的结果如下所示:
java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.lang.Long
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.terminal.dao.impl.ContentDaoImpl.checkThatImagesAreModerated(ContentDaoImpl.java:105)
at com.terminal.service.impl.ContentServiceImpl.checkThatImagesAreModerated(ContentServiceImpl.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
.....
休眠日志:
Hibernate:
select
userconten0_.content_id as col_0_0_
from
user_content userconten0_
where
(
userconten0_.content_id in (
?
)
)
and userconten0_.moderation_status<>'TRUE'
我不明白 hibernate 尝试转换的位置和原因 Long[] to Long
P.S.
使用query.uniqueResult(),列表方法returns一个结果列表,即使只有一个。内部映射可能会将其解析为数组
尝试 query.setParameterList("imageIdList", imageIds);
而不是 query.setParameter("imageIdList", imageIds);
。当为 in
子句绑定参数时,您通常需要使用它。查看 docs for Query.java