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

桔子大人 发布于 2016/04/23 20:41
阅读 753
收藏 0

用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


加载中
1
Sub
Sub

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 对象,不能是其他的。

0
hello123
hello123
String hql="from Teacher t where t.tno=?";
这样写就可以了呀

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=?";
你上面写法返回值类型为:Object[]
0
daixilong930803
daixilong930803
你返回出来的是object[],你在执行query的时候可以这么写Query q=session.createsqlQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
0
Mr________Miss
Mr________Miss
getSession().createSqlQuery(hql).addEntity(Teacher.class)
返回顶部
顶部