java.lang.Long cannot be cast to java.lang.String
publicCollectiongetOptions(Stringkey)throwsException{StringBuilderHql=newStringBuilde...
public Collection getOptions(String key)throws Exception
{
StringBuilder Hql=new StringBuilder()
.append("select new list(d.dname as dname,d.did as did)")
.append(" from Department d ")
.append(" where d.dcode=?")
;
List<List> items=this.getHibernateTemplate().find(Hql.toString(), new Object[]{key});
int size=items.size();
System.out.println("items is ========"+items);
Collection coll=new ArrayList();
for(List<String> item:items)
{ System.out.println("items is ========"+items.get(0));
coll.add(new LabelValueBean(item.get(0),item.get(1)));
}
return coll;
}
coll.add(new LabelValueBean(item.get(0),item.get(1)));
这行总报错。。。。我很不解。。。。谁能详细说说 再帮我改改~~~会加分哦
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 展开
{
StringBuilder Hql=new StringBuilder()
.append("select new list(d.dname as dname,d.did as did)")
.append(" from Department d ")
.append(" where d.dcode=?")
;
List<List> items=this.getHibernateTemplate().find(Hql.toString(), new Object[]{key});
int size=items.size();
System.out.println("items is ========"+items);
Collection coll=new ArrayList();
for(List<String> item:items)
{ System.out.println("items is ========"+items.get(0));
coll.add(new LabelValueBean(item.get(0),item.get(1)));
}
return coll;
}
coll.add(new LabelValueBean(item.get(0),item.get(1)));
这行总报错。。。。我很不解。。。。谁能详细说说 再帮我改改~~~会加分哦
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 展开
展开全部
一、原因:
1)在Hibernate2.0之前版本list.get(0)返回的是Integer类型.
2)在Hibernate3.0以后版本list.get(0)返回的是Long类型,不可以由Long型强转成Integer类型.
3)Integer属于不可更改类型,而且Long和Integer没有任何继承关系。
二、例如:
注: super.pageQuery(hql,null,null,null);调用了一个父类的一个封装方法.查询时候使用
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){});
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
return (Integer) list.get(0);
}
三、解决办法:
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
Number num = (Number) list.get(0);
return num.intValue();
}
注:java.lang.Number是Integer,Long的父类.
1)在Hibernate2.0之前版本list.get(0)返回的是Integer类型.
2)在Hibernate3.0以后版本list.get(0)返回的是Long类型,不可以由Long型强转成Integer类型.
3)Integer属于不可更改类型,而且Long和Integer没有任何继承关系。
二、例如:
注: super.pageQuery(hql,null,null,null);调用了一个父类的一个封装方法.查询时候使用
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){});
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
return (Integer) list.get(0);
}
三、解决办法:
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
Number num = (Number) list.get(0);
return num.intValue();
}
注:java.lang.Number是Integer,Long的父类.
展开全部
类LabelValueBean里的变量是String类型的,item.get(0)是Long型的,需要一下类型转换吧:
coll.add(new LabelValueBean(String.valueof(item.get(0)),String.valueof(item.get(1))));
具体还要看错误信息的。。
coll.add(new LabelValueBean(String.valueof(item.get(0)),String.valueof(item.get(1))));
具体还要看错误信息的。。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
抱错的内容给我们复制一下哈,这样更容易分析出来啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询