报错情况如下
(一)
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
Column 'type' cannot be null
could not execute statement
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
File: org/hibernate/exception/internal/SQLExceptionTypeDelegate.java
Line number: 59
表单提交数据,type表有id和type两个属性。
//jsp页面表单:
<form action="${pageContext.request.contextPath}/manage/dishesAction!saveDishes.action" method="post" enctype="multipart/form-data">
<table align="center" border="1" cellspacing="0" cellpadding="0">
<tr>
<td class="left">编号:</td>
<td class="right"><input type="hidden" name="dishes.id" value="${dishes.id}"/></td>
</tr>
<tr>
<td class="left">菜品名称:</td>
<td class="right"><input type="text" name="dishes.name" value="${dishes.name}"/></td>
</tr>
<tr>
<td class="left">原 料:</td>
<td class="right"><input type="text" name="dishes.recipe" value="${dishes.recipe}"/></td>
</tr>
<tr>
<td class="left">市场价格:</td>
<td class="right"><input type="text" name="dishes.price" value="${dishes.price}" /></td>
</tr>
<tr>
<td class="left">会员价格:</td>
<td class="right"><input type="text" name="dishes.priceAssociator" value="${dishes.priceAssociator}"/></td>
</tr>
<tr>
<td class="left">说 明:</td>
<td class="right"><textarea name="dishes.brief" value="${dishes.brief}"rows="10" cols="30"></textarea></td>
</tr>
<tr>
<td class="left">菜品类别:</td>
<td class="right">
<select name="dishes.types.id">
<c:forEach items="${typesList}" var="types">
<option value="${types.id}">${types.type}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td class="left">上传图片:</td>
<td class="right"><input type="file" name="dishes.imgpath" value="${dishes.imgpath}"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" /></td>
</tr>
</table>
</form>
虽然点击提交网页无法跳转并且给出异常信息,但是通过后台发现数据提交到了后台:
但是不能正常的跳转。
ps:上面的下拉菜单框本来应该是提交type值,但是这样的话报错无法提交,错误百出,等自己学懂了一点再来改笔记。
找资料,查看自己的hbm.xml的实体关系映射
发现dishes与types多对一的关系,hibernate级联设置成了:
<many-to-one name="types" column="typeId" class="Types" cascade="save-update,persist" lazy="false"></many-to-one>
把级联设置成none就可以了。虽然提交的type为null,但是不碍事,页面能跳转了。数据也添加到后台了,至于提交的数据中type为null,没事有id就行。(不设置成null)
参考资料:https://www.cnblogs.com/czb2580/p/6443304.html