JFinal Ctrl中 开启事物 有外键保存报错问题

leon_rock 发布于 2014/07/03 14:23
阅读 637
收藏 0
在controller里面 开启事物 Tx.class
然后先保存一个表a,然后再保存表b,表b中有先前保持表a的id外键
报错:
Caused by: com.jfinal.plugin.activerecord.ActiveRecordException: com.jfinal.plugin.activerecord.ActiveRecordException: The attribute name is not exists: id

加载中
0
JFinal
JFinal

引用来自“小鲅鱼”的评论

@RequiresPermissions("category:i18n")
	@Before({ CategoryI18nEditValidator.class, Tx.class })
	public void i18n() {

...

ACategoryModel acm = new ACategoryModel();
			acm.set(ACategoryModel.NAME, name);
			acm.set(ACategoryModel.PARENT_ID, parentId);
			acm.set(ACategoryModel.DESCRIPTION, StringEscapeUtils.escapeHtml4(description));
			acm.set(ACategoryModel.TYPE, ACategoryModel.TYPE_I18N);
			acm.save();

			ACategoryLanguageModel categoryLanguage = new ACategoryLanguageModel();
			categoryLanguage.set(ACategoryLanguageModel.CATEGORY_ID, acm.getInt(ACategoryModel.ID));
			categoryLanguage.set(ACategoryLanguageModel.PARENT_ID, id);
			categoryLanguage.set(ACategoryLanguageModel.LANGUAGE_ID, languageId);
			categoryLanguage.save();
...
}



没看出有什么问题,可能是 id 没有 set 值进去,检查 id 的名称是否为 "id" ,如果不是,则需要 arp.addMapping("tableName", "id_name", ModelClass.class); 
leon_rock
leon_rock
回复 @JFinal : 是的 正常了~~ thx
JFinal
JFinal
回复 @小鲅鱼 : 现在完全正常了吧?
leon_rock
leon_rock
回复 @JFinal : 刚才看了一下数据库,发现是id出问题,不知谁把id改成int了,郁闷
JFinal
JFinal
回复 @小鲅鱼 : id 使用程序来生成可以搞定
leon_rock
leon_rock
回复 @JFinal : 去掉事务是可以的
下一页
1
蓝水晶飞机
蓝水晶飞机
addMapping("table_name", "primary_key_name", YourTable.class)
0
JFinal
JFinal
贴代码出来
leon_rock
leon_rock
主要是事务提交问题 在Ctrl下面 使用Tx.class 可不可手动提交事物
0
leon_rock
leon_rock
@RequiresPermissions("category:i18n")
	@Before({ CategoryI18nEditValidator.class, Tx.class })
	public void i18n() {

...

ACategoryModel acm = new ACategoryModel();
			acm.set(ACategoryModel.NAME, name);
			acm.set(ACategoryModel.PARENT_ID, parentId);
			acm.set(ACategoryModel.DESCRIPTION, StringEscapeUtils.escapeHtml4(description));
			acm.set(ACategoryModel.TYPE, ACategoryModel.TYPE_I18N);
			acm.save();

			ACategoryLanguageModel categoryLanguage = new ACategoryLanguageModel();
			categoryLanguage.set(ACategoryLanguageModel.CATEGORY_ID, acm.getInt(ACategoryModel.ID));
			categoryLanguage.set(ACategoryLanguageModel.PARENT_ID, id);
			categoryLanguage.set(ACategoryLanguageModel.LANGUAGE_ID, languageId);
			categoryLanguage.save();
...
}



0
leon_rock
leon_rock

引用来自“小鲅鱼”的评论

@RequiresPermissions("category:i18n")
	@Before({ CategoryI18nEditValidator.class, Tx.class })
	public void i18n() {

...

ACategoryModel acm = new ACategoryModel();
			acm.set(ACategoryModel.NAME, name);
			acm.set(ACategoryModel.PARENT_ID, parentId);
			acm.set(ACategoryModel.DESCRIPTION, StringEscapeUtils.escapeHtml4(description));
			acm.set(ACategoryModel.TYPE, ACategoryModel.TYPE_I18N);
			acm.save();

			ACategoryLanguageModel categoryLanguage = new ACategoryLanguageModel();
			categoryLanguage.set(ACategoryLanguageModel.CATEGORY_ID, acm.getInt(ACategoryModel.ID));
			categoryLanguage.set(ACategoryLanguageModel.PARENT_ID, id);
			categoryLanguage.set(ACategoryLanguageModel.LANGUAGE_ID, languageId);
			categoryLanguage.save();
...
}



引用来自“JFinal”的评论

没看出有什么问题,可能是 id 没有 set 值进去,检查 id 的名称是否为 "id" ,如果不是,则需要 arp.addMapping("tableName", "id_name", ModelClass.class); 
发现有人把表中的id 改成int,改成id后正常
返回顶部
顶部