升级到Hibernate5.2.17Final时为什么会收到ClassCastException

时间:2019-05-17 18:23:23

标签: java spring hibernate spring-boot hibernate-5.x

我正在将应用程序升级到Spring5和hibernate5.2,有一个BaseDao,它使用SQLQuery将查询激发到数据库并转换结果。但是我已经升级到hibernate5.2.17并想对我的查询使用NativeQuery。我已经更改了代码及其编译方式,但是尝试运行localNativeQuery.list();时却得到了运行时执行 我对于此行的NativeQuery的替代代码是什么

代码是

public List loadTransformedObjectWithNativeSQL(String paramString, Map paramMap, Class paramClass) {

        List result = null;

        NativeQuery localNativeQuery = this.sessionFactory.getCurrentSession().createNativeQuery(paramString);

        Iterator localIterator = paramMap.entrySet().iterator();
        while (localIterator.hasNext())
        {
          Map.Entry localEntry = (Map.Entry)localIterator.next();
          String str = (String)localEntry.getKey();
          Type localType = (Type)localEntry.getValue();
          localNativeQuery.addScalar(str, localType);
        }
        System.out.println("*************SQLQuery[Map]*****************" +localNativeQuery.getQueryString());
        localNativeQuery.setResultTransformer(Transformers.aliasToBean(paramClass));

        return localNativeQuery.list();
    }

我得到的错误是

14:04:26,882 INFO  [stdout] (default task-34) *************SQLQuery[OBJ]*****************Select q.ques_id quesid,q.question questionaire ,q.value_typ quesValueType,a.value_id valueID,a.question_value quesValue,(select count(1) from cost_ques_value c where c.ques_id=q.ques_id) noOfAnswer, nvl(v.value_id,'0') modelQuesValueID from cost_questionaire q,cost_ques_value a, cost_mdl_ver_t_c_ques_value v where q.ques_id=a.ques_id and q.Category_level=? and q.ques_id = v.ques_id(+) and v.mdl_ver_id(+)=? and q.rec_status_ind=1 and a.rec_status_ind=1 order by q.ques_id
14:07:55,822 ERROR [stderr] (default task-34) java.lang.ClassCastException: com.ge.energy.common.data.config.ModelData cannot be cast to java.util.Map
14:07:55,849 ERROR [stderr] (default task-34)   at org.hibernate.property.access.internal.PropertyAccessMapImpl$SetterImpl.set(PropertyAccessMapImpl.java:102)
14:07:55,849 ERROR [stderr] (default task-34)   at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:85)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:430)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.loader.Loader.list(Loader.java:2502)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
14:07:55,850 ERROR [stderr] (default task-34)   at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2200)
14:07:55,851 ERROR [stderr] (default task-34)   at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
14:07:55,851 ERROR [stderr] (default task-34)   at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
14:07:55,851 ERROR [stderr] (default task-34)   at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
14:07:55,851 ERROR [stderr] (default task-34)   at com.gee.gecs.cosmos.dao.BaseDao.loadTransformedObjectWithNativeSQL(BaseDao.java:226)
14:07:55,851 ERROR [stderr] (default task-34)   at com.gee.gecs.cosmos.dao.config.Con

0 个答案:

没有答案