NutzCN Logo
问答 nutz的dao中 Sqls 的问题。
发布于 2104天前 作者 fangjingping 1580 次浏览 复制 上一个帖子 下一个帖子
标签:
Sql sql = Sqls.create("SELECT * FROM apc_question WHERE question_group = @questionGroup GROUP BY RAND() LIMIT 1");
			sql.params().set("questionGroup", questionNumber+"");
			dao.execute(sql);
			sql.getList(Question.class);
			List<Question> questionList = sql.getList(Question.class);

questionList,为什么 是null。

@Table("apc_question")
public class Question extends BaseBean{

	@Name()
	@Column
	private String question_code;
	
	@Column("question_content")
	private String questionContent;
	
	@Column("question_group")
	private String questionGroup;
	
	@Column("question_type")
	private String questionType;

	public String getQuestion_code() {
		return question_code;
	}

	public void setQuestion_code(String question_code) {
		this.question_code = question_code;
	}

	public String getQuestionContent() {
		return questionContent;
	}

	public void setQuestionContent(String questionContent) {
		this.questionContent = questionContent;
	}

	public String getQuestionGroup() {
		return questionGroup;
	}

	public void setQuestionGroup(String questionGroup) {
		this.questionGroup = questionGroup;
	}

	public String getQuestionType() {
		return questionType;
	}

	public void setQuestionType(String questionType) {
		this.questionType = questionType;
	}
	
	
}

实际上是有一个数据的。

6 回复

因为没设置callback,再看看自定义sql的文档吧

设置怎么样的callback就返回怎样的数据,不设置就没有值了,也就是null了

Sql sql = Sqls.create("SELECT * FROM apc_question WHERE question_group = @questionGroup GROUP BY RAND() LIMIT 1");
			sql.params().set("questionGroup", questionNumber+"");
			sql.setCallback(Sqls.callback.entities());
			dao.execute(sql);
			List<Question> questionList = sql.getList(Question.class);

目前情况有报错。如下:

[WARN ] 18:53:18.560 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/question/get :
org.nutz.dao.DaoException: java.lang.RuntimeException: SQL without entity : SELECT * FROM apc_question WHERE question_group = '1' GROUP BY RAND() LIMIT 1
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:92)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:81)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:248)
	at org.nutz.dao.impl.DaoSupport.execute(DaoSupport.java:236)
	at org.nutz.dao.impl.NutDao.execute(NutDao.java:973)
	at com.xueersi.apc.service.QuestionService.getQuestionsWithContext(QuestionService.java:73)
	at com.xueersi.apc.service.QuestionService$$FASTCLASS._invoke(QuestionService.java:1)
	at org.nutz.lang.reflect.AbstractFastClass.invoke(AbstractFastClass.java:143)
	at org.nutz.lang.reflect.FastClassFactory.invoke(FastClassFactory.java:67)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:27)
	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:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: SQL without entity : SELECT * FROM apc_question WHERE question_group = '1' GROUP BY RAND() LIMIT 1
	at org.nutz.lang.Lang.makeThrow(Lang.java:132)
	at org.nutz.dao.impl.sql.callback.EntityCallback.invoke(EntityCallback.java:20)
	at org.nutz.dao.impl.sql.NutSql.onAfter(NutSql.java:135)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:277)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:53)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:158)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:125)
	... 37 more

加上

sql.setEntity(dao.getEntity(Question.class));

//push question
			Sql sql = Sqls.create("SELECT * FROM apc_question WHERE question_group = @questionGroup GROUP BY RAND() LIMIT 1");
			sql.params().set("questionGroup", questionNumber+"");
			sql.setCallback(Sqls.callback.entities());
			sql.setEntity(dao.getEntity(Question.class));
			dao.execute(sql);
			List<Question> questionList = sql.getList(Question.class);

我是想说,已经解决了。来回归下问题。 楼上是正确使用方法。

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