插入记录时出现ConstraintViolationException:could not insert 异常
我用的是Hibernate3在向数据库插入记录时,出现ConstraintViolationException:could not insert异常,但我检查了,要插入的数据满足所有的约束条件,而且向其他表插入记录都没有问题,就只这一个表有问题。
在hbm.xml中ID的配置如下:
程序代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate. package="pbc.bz.intendance.model"> <class name="Intendance" table="t_intendance" > <!-- 映射标识属性 --> <id name="intendId" column="intend_id" type="java.lang.Integer"> <generator class="identity" /> </id> <!-- 下面映射其他普通属性 --> <property name="projectName" column="project_name" type="string" not-null="true" lazy="false" /> <property name="checkDate" column="check_date" type="date" not-null="true" lazy="false" /> <property name="transactor" column="transactor" type="string" not-null="true" lazy="false" /> <property name="beChecked" column="be_checked" type="string" not-null="true" lazy="false" /> <property name="problemKind" column="problem_kind" type="string" not-null="true" lazy="false" /> <property name="problemDesc" column="problem_desc" type="text" not-null="true" lazy="false" /> <property name="riskKind" column="risk_kind" type="string" not-null="true" lazy="false" /> <property name="controllable" column="controllable" type="text" not-null="true" lazy="false" /> <property name="reason" column="reason" type="string" not-null="true" lazy="false" /> <property name="behindDuty" column="behind_duty" type="string" not-null="true" lazy="false" /> <!-- 映射该Intendance所关联的User: recorder/rechecker/assessor --> <many-to-one name="recorder" column="recorder" class="User" not-null="true" lazy="false" /> <many-to-one name="rechecker" column="rechecker" class="User" lazy="false" /> <many-to-one name="assessor" column="assessor" class="User" lazy="false" /> <!-- 映射该Intendance关联的反馈信息Feedback --> <many-to-one name="back" column="back_id" class="Feedback" lazy="false" /> <!-- 映射该Intendance所处的状态State --> <many-to-one name="stat" column="state_id" class="State" lazy="false"/> <!-- 配置监督信息Intendance与用户User的映射关系(修改关系--多对多) --> <set name="modifier" table="t_modification_of_intend" lazy="false" inverse="true"> <key column="intend_id" /> <one-to-many class="ModificationOfIntendance" /> </set> <property name="submitDate" column="submit_date" type="timestamp" not-null="true" lazy="false" /> </class> </hibernate-mapping>其中,rechecker, assessor, back三个属性可以为空,在插入之前,我将其设置为null.
添加监督信息的action函数:
程序代码:
// 说明,该action使用了模型驱动方式,即实现了com.opensymphony.xwork2.ModelDriven 接口。 // 添加监督信息 public String addIntendance() throws Exception { Map session = ActionContext.getContext().getSession(); String ver2 = (String)session.get("randomString"); session.put("randomString", null); // if (vercode.equalsIgnoreCase(ver2)) { // 取得当前登录的用户信息 User cu = (User)session.get("the_user"); System.out.println("当前User ID: " + cu.getUserId()); model.setRecorder(cu); model.setRechecker(null); model.setAssessor(null); model.setBack(null); model.setStat(mgr.getTheState(1)); model.setSubmitDate(new Date()); System.out.println("*********************"); System.out.println(model.getStat().getStateId()); System.out.println(model.getStat().getStateName()); System.out.println(model.getStat().getStateDesc()); System.out.println("*********************"); System.out.println("*********************"); System.out.println(model.getProjectName()); System.out.println(model.getCheckDate()); System.out.println(model.getTransactor()); System.out.println(model.getBeChecked()); System.out.println(model.getProblemKind()); System.out.println(model.getProblemDesc()); System.out.println(model.getRiskKind()); System.out.println(model.getControllable()); System.out.println(model.getReason()); System.out.println(model.getBehindDuty()); System.out.println(model.getRecorder().getUserId()); System.out.println(model.getRechecker()); System.out.println(model.getAssessor()); System.out.println(model.getBack()); System.out.println(model.getStat().getStateId()); System.out.println(model.getSubmitDate()); System.out.println("*********************"); //return SUCCESS; if (mgr.addIntendance(model) > 0) { addActionMessage("成功添加监督信息!"); System.out.println("执行到这里了..."); return SUCCESS; } else { addActionError("添加监督信息失败,请重试!"); return "failure"; } } else { addActionError("验证码不匹配,请重新输入!"); return "failure"; } }函数中System.out.println()输出的数据显示都满足数据库和配置文件的约束条件。
在struts.xml文件中的配置
程序代码:
<!-- 添加监督信息 --> <action name="addIntendance" class="pbc.bz.intendance.action.IntendanceAction" method="addIntendance"> <result name="input">/intend/addintend.jsp</result> <result name="failure">/intend/addintend.jsp</result> <result name="success">/users/welcome.jsp</result> </action>
每次在提交表单到addIntendance后,在TOMCAT的控制台都能看到要提交的每项数据,可就是在向数据库添加的时候出现ConstraintViolationException:could not insert 异常。
在网上找的情况都与我这里不一样,
有人说把<generator class="identity"/> 改为 <generator class="assigned"/>
我试了,还是不行。
请问有什么解决方法?
[ 本帖最后由 无缘今生 于 2010-9-18 14:38 编辑 ]