很奇怪,执行所有表的增加时都没有报错,唯独这个报错。这是什么原因啊?在网上也找了些,可是都不对!
有人说把 IDENTITY_INSERT 设置为 ON 时 ,那在哪里设置呢?怎么设置啊?
更奇怪的是任何值都不设置,直接插入对象,居然还报错。而且还是一样的效果。
我的Action类
public class SendCommentsAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
KmsComments commbean = new KmsComments();
String questionId = request.getParameter("questionId");
String commentsContent = request.getParameter("commentsContent");
FindId find = new FindId();
commbean.setFindId(Integer.parseInt(questionId));
// 获取发表的内容
commbean.setAnswer(commentsContent);
// 设置回复时间
commbean.setAnswerTime(new Date(System.currentTimeMillis()));
// ==假设用户的id号为1================
int userId = 1;
// ===============
KmsUser userbean = find.findUserByuserId(userId);
commbean.setKmsUser(userbean);
try {
DisDaoImpl disdaoimpl = new DisDaoImpl();
// 调用DisDaoImpl类的[remessage()]回复问题
boolean isOk = disdaoimpl.remessage(commbean);
if (isOk) {
return mapping.findForward("sendComments_success");
}
} catch (Exception ex) {
ex.printStackTrace();
}
// 回复失败
return mapping.findForward("sendComments_fail");
}
}
对应POJO类的.hbm.xml文件
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="cn.ks.hulu.bean.KmsComments" table="kms_comments" schema="dbo" catalog="KnowledgeSystem">
<id name="commentsId" type="java.lang.Integer">
<column name="CommentsId" />
<generator class="native" />
</id>
<many-to-one name="kmsUser" class="cn.ks.hulu.bean.KmsUser" fetch="select">
<column name="UserId" />
</many-to-one>
<property name="commentsType" type="java.lang.Integer">
<column name="CommentsType" />
</property>
<property name="findId" type="java.lang.Integer">
<column name="FindId" />
</property>
<property name="answer" type="java.lang.String">
<column name="Answer" length="200" />
</property>
<property name="answerTime" type="java.util.Date">
<column name="AnswerTime" length="23" />
</property>
<many-to-one name="kmsQuestion" column="commentsId"></many-to-one>
</class>
</hibernate-mapping>
这是异常代码:红色部分为hibernate自动增加?为什么啊?
org.hibernate.exception.ConstraintViolationException: could not insert: [cn.ks.hulu.bean.KmsComments]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.ks.hulu.disdaoimpl.DisDaoImpl.remessage(DisDaoImpl.java:81)
at com.yourcompany.struts.action.SendCommentsAction.execute(SendCommentsAction.java:70)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'kms_comments' 中的标识列插入显式值。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1953)
... 36 more
[此贴子已经被作者于2007-8-21 17:46:15编辑过]