当前位置: 代码迷 >> Java Web开发 >> hibernate 执行插入时报错
  详细解决方案

hibernate 执行插入时报错

热度:423   发布时间:2007-08-21 16:45:14.0
hibernate 执行插入时报错

很奇怪,执行所有表的增加时都没有报错,唯独这个报错。这是什么原因啊?在网上也找了些,可是都不对!

有人说把 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文件

<?xml version="1.0" encoding="utf-8"?>
<!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自动增加?为什么啊?

Hibernate: insert into KnowledgeSystem.dbo.kms_comments (UserId, CommentsType, FindId, Answer, AnswerTime, commentsId) values (?, ?, ?, ?, ?, ?) select scope_identity()
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编辑过]

搜索更多相关主题的帖子: hibernate  

----------------解决方案--------------------------------------------------------
刚才我发现hibernate自动产生insert语句中有插入主自动增长值,可问题是我跟本就没有设置过,为什么hibernate会这样弄错。
----------------解决方案--------------------------------------------------------
<generator class="native" />改为
<generator class="assigned" />试试看呢
这样应该不会自动增加了
----------------解决方案--------------------------------------------------------
根据我的经验,自动增长列的"?"可以不用加,加了会报错.


----------------解决方案--------------------------------------------------------
我说老兄,你指的是哪里的“?”啊,你不会指的是这条语句吧,这是hibernate自动产生的。[CODE]insert into KnowledgeSystem.dbo.kms_comments (UserId, CommentsType, FindId, Answer, AnswerTime, commentsId) values (?, ?, ?, ?, ?, ?) select scope_identity()[/CODE]
我现在想问的是,我没有插入主键的值,可是hibernate为什么会帮我加,害得我出错。
----------------解决方案--------------------------------------------------------
你是关联表?试下先加另一张表的数据,.可能是由于另一个表数据没的所以ID没产生,这个表又要用所以出错找不到ID.
----------------解决方案--------------------------------------------------------

试试3楼的方法


----------------解决方案--------------------------------------------------------

你可以先试一下,单张表插入。从表中id是来源与主表的!可能是主表问题!


----------------解决方案--------------------------------------------------------
  相关解决方案