hibernateexception - 无法实例化resultclass

时间:2014-11-05 14:01:02

标签: java hibernate

我在结果转换过程中遇到以下错误: -

Exception in thread "main" org.hibernate.HibernateException: Could not instantiate resultclass: com.javatpoint.mypackage.EmployeeDTO
    at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:71)
    at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:69)
    at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:330)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    at org.hibernate.loader.Loader.list(Loader.java:2090)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)
    at com.javatpoint.mypackage.StoreData.unionqry(StoreData.java:54)
    at com.javatpoint.mypackage.StoreData.main(StoreData.java:19)


public static void unionqry(){
    Session session = new AnnotationConfiguration().configure()
            .buildSessionFactory().openSession();

    Transaction t = session.beginTransaction();

    String sql = "SELECT firstName, lastName FROM EMP1000 union Select firstName, lastName from Emp1000";
    //String sql = "SELECT a.id,b.emp_dept FROM test.emp1000 a inner join emp_dept b on a.id = b.id";
    SQLQuery query = session.createSQLQuery(sql);
    List result = query.addScalar("firstName").addScalar("lastName").setResultTransformer(Transformers.aliasToBean(EmployeeDTO.class)).list();
    Employee e = (Employee)result.get(0);

Tranformclass:

class EmployeeDTO{
    private String firstName;
    private String lastName;
    public EmployeeDTO(){}

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎正在使用注释映射而不是使用xml映射
如果是这种情况,则应在POJO类(EmployeeDTO)中提供映射信息。

检查以下示例: example-1 example-2有待进一步说明。