NutzCN Logo
问答 @many 关系中insertWith关联插入问题
发布于 107天前 作者 qq_4a89c7c9 102 次浏览 复制 上一个帖子 下一个帖子
标签:
public class Wjdc_wj_info extends Model implements Serializable {

	@Column
	@Id
	private Long wj_id;
	
	// 题型列表
	@Many(field = "wj_id")
	private List<Wjdc_wj_question_base> baseList;

public class Wjdc_wj_question_base implements Serializable { private static final long serialVersionUID = 1L; @Column @Id private Integer question_id; @Column @Comment("问卷id") @ColDefine(type = ColType.INT) private Long wj_id; // 组合题中的title属性 @Many(field = "question_id") private List<Wjdc_wj_question_base_title> titleList; @Many(field = "question_id") private List<Wjdc_wj_question_base_option> optionList;

Wjdc_wj_info中有Wjdc_wj_question_base列表,many关系,而Wjdc_wj_question_base里还对应Wjdc_wj_question_base_title列表和Wjdc_wj_question_base_option列表。

wjInfoService.insertWith(wjdcWjInfo,"baseList");

Wjdc_wj_info的实体中,已经按照这个层次关系封装好数据,如上方式,可以将Wjdc_wj_question_base的数据插入到对应表中,但是Wjdc_wj_question_base中的Wjdc_wj_question_base_title列表和Wjdc_wj_question_base_option列表,如何同步插入到对应表中?

8 回复
Trans.exec(new Atom() {
    public void run() {
          dao.insertWith(wjdcWjInfo, null);
          dao.insertLinks(wjdcWjInfo.getBaseList(), null);
    }
});

2017-01-12 14:51:01,569 org.nutz.lang.eject.EjectByGetter.eject(EjectByGetter.java:30) INFO  - Fail to value by getter
java.lang.IllegalArgumentException: object is not an instance of declaring class
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

这是什么错,属性都有get方法的

完整报错信息


2017-01-12 15:43:44,779 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) DEBUG - Exception when parser SQL sql, skip cache detect!! SQL=RUN 2017-01-12 15:43:47,968 org.nutz.lang.eject.EjectByGetter.eject(EjectByGetter.java:30) INFO - Fail to value by getter java.lang.IllegalArgumentException: object is not an instance of declaring class 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.lang.eject.EjectByGetter.eject(EjectByGetter.java:23) at org.nutz.dao.impl.entity.field.AbstractEntityField.getValue(AbstractEntityField.java:57) at org.nutz.dao.impl.link.DoInsertLinkVisitor.visit(DoInsertLinkVisitor.java:22) at org.nutz.dao.impl.entity.LinkFieldSet.visit(LinkFieldSet.java:41) at org.nutz.dao.impl.entity.NutEntity.visitMany(NutEntity.java:337) at org.nutz.dao.impl.NutDao.insertLinks(NutDao.java:254) at cn.wjdc.common.base.Service.insertLinks(Service.java:82) at cn.wjdc.modules.controllers.wj.WjInfoController$1.run(WjInfoController.java:121) at org.nutz.trans.Trans.exec(Trans.java:174) at org.nutz.trans.Trans.exec(Trans.java:132) at cn.wjdc.modules.controllers.wj.WjInfoController.addDo(WjInfoController.java:118) at cn.wjdc.modules.controllers.wj.WjInfoController$$NUTZAOP._aop_invoke(WjInfoController.java:2) at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77) at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57) at cn.wjdc.common.services.log.SLogAopInterceptor.filter(SLogAopInterceptor.java:59) at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) at cn.wjdc.modules.controllers.wj.WjInfoController$$NUTZAOP.addDo(WjInfoController.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 cn.wjdc.common.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:36) at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at cn.wjdc.common.processor.NutShiroProcessor.process(NutShiroProcessor.java:51) 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.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at cn.wjdc.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:35) at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at cn.wjdc.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:19) 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:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at cn.wjdc.common.filter.RouteFilter.doFilter(RouteFilter.java:33) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

似乎遇到bug了, 改成这样试试

for (Objec base : wjdcWjInfo.getBaseList()) P
    dao.insertLinks(base, null);
}

wjInfoService.insertWith(wjdcWjInfo, null);
			    	
			    	 List<Wjdc_wj_question_base> baseList = wjdcWjInfo.getBaseList();
			    	 
			    	 for(Wjdc_wj_question_base base:baseList)
			    	 {
			    		 wjInfoService.insertLinks(base,null);
			    	 }

这样ok了。可能我这种结构稍微复杂点。

另外,

@Column
	@Id
	private Integer question_id;

	@Column
	@Comment("问卷id")
	@ColDefine(type = ColType.INT)
	private Long wj_id;
	
	// 组合题中的title属性
	@Many(field = "question_id")
	private List<Wjdc_wj_question_base_title> titleList;

Wjdc_wj_question_base_title其实有两个复合主键的,这种方式,question_id可以入库,但是wj_id不能。试了下@Many貌似不能设置两个field吧

额,后面这个,只能自己update一下了

wjInfoService.insertWith(wjdcWjInfo, null);

					List<Wjdc_wj_question_base> baseList = wjdcWjInfo
							.getBaseList();

					if (CollectionUtil.isNotEmpty(baseList)) {
						for (Wjdc_wj_question_base base : baseList) {

							long wjId = base.getWj_id();

							List<Wjdc_wj_question_base_option> options = base
									.getOptionList();

							if (CollectionUtil.isNotEmpty(options)) {
								for (Wjdc_wj_question_base_option option : options) {
									option.setWj_id(wjId);
								}
							}
							List<Wjdc_wj_question_base_title> titles = base
									.getTitleList();
							if (CollectionUtil.isNotEmpty(titles)) {
								for (Wjdc_wj_question_base_title title : titles) {
									title.setWj_id(wjId);
								}
							}
							wjInfoService.insertLinks(base, null);
						}
					}

通过insertLinks前遍历单独设置wjid的方式,达到了要求

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