一百分解决一个HIBERNATE简单问题
报错提示
Hibernate: select bulletin0_.id as x0_0_ from BULLETIN bulletin0_
java.lang.ClassCastException at meis.tag.Bulletin_tag.doAfterBody(Bulletin_tag.java:48)
at jsp_servlet.__index._jspService(__index.java:202)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run
(ServletStubImpl.java:1006)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
代码
Query query=session.createQuery("select b.id from meis.hibernate.Bulletin as b");
java.util.List list=query.list();
result.append("<TABLE width=\"97%\" border=\"0\" cellspacing=\"6\" cellpadding=\"0\"><tr><td>dfdsfsdf"+list.size()+"</td></tr>");
Iterator iter = list.iterator();
while (iter.hasNext())
{
.....
}
/*
*/
映射类文件
package meis.hibernate;
public class Bulletin {
//发布日期
private String id;
private String issuedate;
private String title;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getIssuedate()
{
return issuedate;
}
public void setIssuedate(String issuedate)
{
this.issuedate = issuedate;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="meis.hibernate.Bulletin" table="BULLETIN">
<id name="id">
<generator class="assigned" />
</id>
<property name="issuedate" />
<property name="title" />
</class>
</hibernate-mapping>
------解决方案--------------------while (iter.hasNext())
{
.....
}
把其中的内容发上来
------解决方案--------------------用数组来解决吧
Object[] bulletin= (Object[]) iter.next();
bulletin[0]
bulletin[1]
------解决方案--------------------meis.hibernate.Bulletin bulletin=(meis.hibernate.Bulletin)iter.next();
转型错误
------解决方案--------------------Query query=session.createQuery("select b.id from meis.hibernate.Bulletin as b");
java.util.List list=query.list(); 中返回的不是Bulletin对象
好好对比一下!
------解决方案--------------------karlpan01 说到:
Query query=session.createQuery("select b.id from meis.hibernate.Bulletin as b");
java.util.List list=query.list(); 中返回的不是Bulletin对象
好好对比一下!
就是这个答案了。你的list里面现在放的是一些id,比如{1,2,3,4},
而不是bulletin的对象{bulletin1,bulletin2,bulletin3,bulletin4}.