开发者社区> 问答> 正文

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也贴出来吧

 @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

展开
收起
爵霸 2016-02-29 14:29:18 2636 0
1 条回答
写回答
取消 提交回答
  • 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=?

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

    select t from Teacher t where t.tno=?

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

    2019-07-17 18:50:17
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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