一百分解决一个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}.
所以在结果list面的元素是不能用 getId()获是getTitle()这些方法的。
[解决办法]
Query query=session.createQuery("select b.id from meis.hibernate.Bulletin as b"); java.util.List list=query.list();
[解决办法]
若你想取部分字段的话,取出来的是List中每个单元里一个对象数组,每个数组其实就是一行记录。例如可以这样:
List ls = session.createQuery("select er.employerid,et.employmentid,et.startDate from Employer er left join er.employments as et") .list(); for (Iterator itr = ls.iterator(); itr.hasNext(); ) { Object[] oo = (Object[])itr.next(); for (int i = 0;i < oo.length ;i++ ) { System.out.println(oo[i]); } System.out.println("===================="); }
[解决办法]
貌似来晚了
java.lang.ClassCastException 是 JVM 在检测到两个类型间的转换不兼容时引发的运行时异常
Query query=session.createQuery("select b.id from meis.hibernate.Bulletin as b");
[color=#FF0000]meis.hibernate.Bulletin bulletin=(meis.hibernate.Bulletin)iter.next();[/color]
类型不同啊~