关于hibernate 分页 类型转换错误

分页代码:

public List getAppointStudent(int start,int pageSize) {
try{
session=sessionFactory.openSession();
Query query=session.createQuery("[color=red]from Students[/color]")
.setFirstResult(start-1)
.setMaxResults(pageSize);
list=query.list();
}catch(Exception e){
e.printStackTrace();

}finally{
session.close();
}
return list;
}

Students 的配置文件















class="pojo.Users"
fetch="join"
cascade="all" />
class="pojo.Dormitorys"
column="dor_id"
fetch="join"
cascade="none"
lazy="false"/>

发现一个问题 就是 返回的list 里面的对象是String 类型,不是我想得到的Students 这个pojo类型,是不是和映射文件设置成关联有关系。

难道一定用 select .... 这样的hql语句 得到一个object[] 在拆分数据,然后在 封装成一个pojo 如果这样我无语了。

[b]问题补充:[/b]
pojo:

public class Students {
private String stu_id;
private String stu_name;
private String stu_sex;
private String stu_age;
private Integer stu_telephone;
private String stu_department;
private String stu_remarks;
private Dormitorys dormitory;
private Users user;

public Dormitorys getDormitory() {
    return dormitory;
}
public void setDormitory(Dormitorys dormitory) {
    this.dormitory = dormitory;
}
public Users getUser() {
    return user;
}
public void setUser(Users user) {
    this.user = user;
}
public String getStu_id() {
    return stu_id;
}
public void setStu_id(String stu_id) {
    this.stu_id = stu_id;
}

public String getStu_name() {
    return stu_name;
}
public void setStu_name(String stu_name) {
    this.stu_name = stu_name;
}
public String getStu_sex() {
    return stu_sex;
}
public void setStu_sex(String stu_sex) {
    this.stu_sex = stu_sex;
}
public String getStu_age() {
    return stu_age;
}
public void setStu_age(String stu_age) {
    this.stu_age = stu_age;
}
public Integer getStu_telephone() {
    return stu_telephone;
}
public void setStu_telephone(Integer stu_telephone) {
    this.stu_telephone = stu_telephone;
}
public String getStu_department() {
    return stu_department;
}
public void setStu_department(String stu_department) {
    this.stu_department = stu_department;
}
public String getStu_remarks() {
    return stu_remarks;
}
public void setStu_remarks(String stu_remarks) {
    this.stu_remarks = stu_remarks;
}

}

异常:

[color=red]java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer[/color] at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:830)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:887)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at db.dao.CountDAO.getAppointStudent(CountDAO.java:49)
at action.StudentsPageAction.execute(StudentsPageAction.java:65)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at servlet.DormitoryServlet.process(DormitoryServlet.java:23)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3496)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)

3个回答

private Integer stu_telephone;

问题就是这里啦.你查询出来的是String 当你赋值到javabean的时候,因为javabean是Integer所以就一次咯.处理下就可以了.电话号码嘛,肯定用String咯
又不用计算,用什么整型?

从代码来看应该不错呀.你把错误信息贴出来看看

list里每个对象是String 类型,不可能吧

你把部分打印出来看下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问