在Hibernate中选择查询不能使用HQL

时间:2016-02-19 10:42:59

标签: hibernate

我尝试使用HQLnate的HQL的select查询来获取结果。然而它抛出一个错误。我的代码和模型类如下。

package com.chni;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Dashboard {

@Override
public String toString() {
    return "Dashboard [uid=" + uid + ", keyname=" + keyname + ", dbvalue="
            + dbvalue + ", getUid()=" + getUid() + ", getKeyname()="
            + getKeyname() + ", getDbvalue()=" + getDbvalue()
            + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
            + ", toString()=" + super.toString() + "]";
}

@Id
@GeneratedValue
private int dbid;

private String uid;

private String keyname;

private String dbvalue;

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getKeyname() {
    return keyname;
}

public void setKeyname(String keyname) {
    this.keyname = keyname;
}

public Dashboard(String uid, String keyname, String dbvalue) {
    super();
    this.uid = uid;
    this.keyname = keyname;
    this.dbvalue = dbvalue;
}

public String getDbvalue() {
    return dbvalue;
}

public void setDbvalue(String dbvalue) {
    this.dbvalue = dbvalue;
}

}

我的代码

 String hql = "FROM com.chni.Dashboard where uid = "+userId;
 Query query = session.createQuery(hql);
 System.out.println(query.list());
 List<Dashboard> listUserDetails = query.list();

 for (Dashboard user : listUserDetails) {
    System.out.println(user.getUid());
 }

错误如下

Feb 19, 2016 4:05:37 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
select
    dashboard0_.dbid as dbid1_0_,
    dashboard0_.dbvalue as dbvalue2_0_,
    dashboard0_.keyname as keyname3_0_,
    dashboard0_.uid as uid4_0_ 
from
    Dashboard dashboard0_ 
where
    dashboard0_.uid=2
org.hibernate.InstantiationException: No default constructor for entity:  :  com.intu.Dashboard
 at   org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:81)
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:101)

我搜索过类似错误的其他堆栈问题,但无法弄清楚确切的问题。我在代码或POJO中遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

只需添加不带参数的构造函数

public Dashboard() {}