开发者社区> 问答> 正文

hql语句查询到的一个Object强制转换为一个pojo不能成功,求助,很急!?报错

用Spring整合Hibernate去实现对mysql数据表信息查询时,由hql语句查询到的一个Object强制转换为一个pojo不能成功。

TeacherDaoImpl.java代码

public Teacher findByNo(String no) {

String hql="select t.tno,t.tname,t.tpassword,t.tgender,t.tbirthday,t.tbelong,t.tduty,t.ttaketime,t.ttel                     from Teacher t where t.tno=?";
Teacher teacher=(Teacher)getSession().createQuery(hql).setParameter(0,no).uniqueResult(); //报错
return teacher;

数据库已连接成功,实体类Teacher也贴出来吧

package a.model;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import  java.util.Date;
@Entity
@Table(name="Teacher")
public class Teacher {


public Teacher() {
super();
// TODO Auto-generated constructor stub
}
@Id
@Column(name="tno")
private String tno;
@Column(name="tname")
    private String tname;
@Column(name="tpassword")
private String tpassword;

@Column(name="tgender")
    private String tgender;
@Column(name="tbirthday")
    private Date tbirthday;
@Column(name="tbelong")
    private String tbelong;
@Column(name="tduty")
    private String tduty;
@Column(name="ttaketime")
    private Date ttaketime;
    @Column(name="ttel")
    private String ttel;
    
   

public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTgender() {
return tgender;
}
public void setTgender(String tgender) {
this.tgender = tgender;
}

public String getTbelong() {
return tbelong;
}
public void setTbelong(String tbelong) {
this.tbelong = tbelong;
}
public String getTduty() {
return tduty;
}
public void setTduty(String tduty) {
this.tduty = tduty;
}

public String getTtel() {
return ttel;
}
public void setTtel(String ttel) {
this.ttel = ttel;
}
public Date getTbirthday() {
return tbirthday;
}
public void setTbirthday(Date tbirthday) {
this.tbirthday = tbirthday;
}
public Date getTtaketime() {
return ttaketime;
}
public void setTtaketime(Date ttaketime) {
this.ttaketime = ttaketime;
}
public String getTpassword() {
return tpassword;
}
public void setTpassword(String tpassword) {
this.tpassword = tpassword;
}
@Override
public String toString() {
return "Teacher [tno=" + tno + ", tname=" + tname + ", tpassword=" + tpassword + ", tgender=" + tgender
+ ", tbirthday=" + tbirthday + ", tbelong=" + tbelong + ", tduty=" + tduty + ", ttaketime=" + ttaketime
+ ", ttel=" + ttel + "]";
}




}


报错

HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to a.model.Teacher


展开
收起
爱吃鱼的程序员 2020-06-09 15:38:11 643 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    HQL语句不对,

    selectt.tno,t.tname,t.tpassword,t.tgender,t.tbirthday,t.tbelong,t.tduty,t.ttaketime,t.ttel fromTeachertwheret.tno=?

    这个只能出来object[],要出Teacher对象,只能

    selecttfromTeachertwheret.tno=?

    其中select只能是Teacher对象,不能是其他的。

    Stringhql="fromTeachertwheret.tno=?";
    这样写就可以了呀

    Stringhql="selectt.tno,t.tname,t.tpassword,t.tgender,t.tbirthday,t.tbelong,t.tduty,t.ttaketime,t.ttel           fromTeachertwheret.tno=?";
    你上面写法返回值类型为:Object[]
    你返回出来的是object[],你在执行query的时候可以这么写Queryq=session.createsqlQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); getSession().createSqlQuery(hql).addEntity(Teacher.class)
    2020-06-09 15:38:28
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载