org.hibernate.HibernateException:无法实例化resultclass

时间:2012-08-09 20:00:19

标签: java spring hibernate

完整堆栈跟踪:

org.hibernate.HibernateException: Could not instantiate resultclass: com.firstbankpr.lmu.data.dto.EmployeeScore
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:99)
at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:96)
at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:361)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at com.nimchip.lmu.data.dao.hibernate.WinnerHibernateDAO.findEmployeeTotals(WinnerHibernateDAO.java:155)
at com.nimchip.lmu.data.dao.WinnerDAOTest.testEmpScore(WinnerDAOTest.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

第11行指向此查询的最后一行:

        Query query = currentSession().createSQLQuery(
            "select n.EMP_ID as id, SUM(DISTINCT(nom.TOTAL_POINT)) as score from" +
                    " NOMINEE n join NOMINATION nom on nom.NOM_ID = n.NOM_ID" +
                    " join EMPLOYEE e on n.EMP_ID = e.EMP_ID" +
                    " join COMPANY c on c.COMPANY_CODE = e.COMPANY_CODE" +
                    " join REGION r on r.REGION_ID = c.REGION_ID" +
                    " where nom.PERIOD_ID = :periodId" +
                    " AND nom.STATUS_ID = 2" +
                    " AND e.ISACTIVE = 1" +
                    " AND nom.CATEGORY_CODE != 'H'" +
                    " AND r.REGION_ID = :regionId" +
                    " group by n.EMP_ID")
            .addScalar("id")
            .addScalar("score")
            .setParameter("regionId", regionId)
            .setParameter("periodId", periodId)
            .setResultTransformer(Transformers.aliasToBean(EmployeeScore.class));

这是EmployeeScore.class:

public class EmployeeScore {

    private int id;
    private int score;

    EmployeeScore(){}

    public EmployeeScore(int id, int score) {
        this.id = id;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EmployeeScore that = (EmployeeScore) o;

        if (id != that.id) return false;
        if (score != that.score) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + score;
        return result;
    }
}

构造者在,为什么不工作?注意:我也使用Spring,这是一个DTO bean。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:41)

EmployeeScore的无参数构造函数应该是公共的:

public EmployeeScore(){}

答案 1 :(得分:8)

您可能会收到此错误的另一个原因是因为它是您正在尝试转换的内部类,代码会编译,但您会收到同样的错误。