NutzCN Logo
问答 dao的自定义sql中,sql对象的返回值,自定义的类型不可以是JSONObject这种类型吗?必须是实体类吗?
发布于 3149天前 作者 mingkaili 2732 次浏览 复制 上一个帖子 下一个帖子
标签:
代码如下:     
    StringBuffer sbSql = new StringBuffer();
    sbSql.append("select *");
    sbSql.append("  from mv_qizhou_search q ");

    Sql sql = Sqls.queryEntity(sbSql.toString());
    sql.setCallback(new SqlCallback() {
       @Override
       public Object invoke(Connection conn, ResultSet rs, Sql sql)
          throws SQLException {
         List<JSONObject> list = new ArrayList<JSONObject>();
         while (rs.next()) {
          JSONObject obj = new JSONObject();
          obj.put("id", rs.getString("id"));
          obj.put("oldThumb", rs.getString("oldThumb"));
          obj.put("oldUrl", rs.getString("oldUrl"));
          obj.put("oldSmall", rs.getString("oldSmall"));
          list.add(obj);
         }
         return list;
       }
    });

** dao.execute(sql);**
sql.getList(JSONObject.class);

    执行中,加粗的地方报错。org.nutz.dao.DaoException: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
    应该是jsonObject,没有反射成功。
4 回复

既然是自行setCallback, 那么就不需要用queryEntity

Sql sql = Sqls.create(sbSql.toString());
sql.setCallback(new SqlCallback() {
   // ....
});
dao.execute(sql);
List<JSONObject> list = sql.getList(JSONObject.class);

但具体报错的原因,应该跟queryEntity无关. 不晓得你说的 "没有反射成功" 指的是啥, 请贴出完整的报错信息

PS: 我觉得Sqls.callback.maps() 已经足够你使用.

报错如下:
org.nutz.dao.DaoException: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:49)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:306)
at org.nutz.trans.Trans.exec(Trans.java:174)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:276)
at org.nutz.dao.impl.DaoSupport.execute(DaoSupport.java:235)
at org.nutz.dao.impl.NutDao.execute(NutDao.java:983)
at com.mxlvniao.lvniao.qizhou.QizhouDao.selCommForUpdComm(QizhouDao.java:208)
at com.mxlvniao.lvniao.qizhou.QizhouDao$$NUTZAOP._aop_invoke(QizhouDao.java:3)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.qizhou.QizhouDao$$NUTZAOP.selCommForUpdComm(QizhouDao.java:1)
at com.mxlvniao.lvniao.qizhou.QizhouService.syncQizhouData(QizhouService.java:253)
at com.mxlvniao.lvniao.qizhou.QizhouService$$NUTZAOP._aop_invoke(QizhouService.java:2)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.qizhou.QizhouService$$NUTZAOP.syncQizhouData(QizhouService.java:1)
at com.mxlvniao.lvniao.qizhou.QizhouAction.syncQizhouData(QizhouAction.java:28)
at com.mxlvniao.lvniao.qizhou.QizhouAction$$NUTZAOP._aop_invoke(QizhouAction.java:2)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.qizhou.QizhouAction$$NUTZAOP.syncQizhouData(QizhouAction.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
at net.sf.json.JSONObject.element(JSONObject.java:1786)
at net.sf.json.JSONObject.element(JSONObject.java:1764)
at net.sf.json.JSONObject.put(JSONObject.java:2380)
at com.mxlvniao.lvniao.qizhou.QizhouDao$1.invoke(QizhouDao.java:188)
at org.nutz.dao.impl.sql.NutSql.onAfter(NutSql.java:134)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:233)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:45)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:38)
... 62 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155)
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1323)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:749)
... 76 more
Caused by: java.lang.IllegalArgumentException
at java.sql.Date.getHours(Unknown Source)
... 86 more

这是哪一行??

at com.mxlvniao.lvniao.qizhou.QizhouDao$1.invoke(QizhouDao.java:188)

@wendal 原来在下面,我找到问题了。谢哥。

添加回复
请先登陆
回到顶部