| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1022 人关注过本帖
标题:hibernate 执行插入时报错
只看楼主 加入收藏
huluhulu88
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-5-19
收藏
 问题点数:0 回复次数:7 
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 
2007-08-21 16:45
huluhulu88
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-5-19
收藏
得分:0 
刚才我发现hibernate自动产生insert语句中有插入主自动增长值,可问题是我跟本就没有设置过,为什么hibernate会这样弄错。

2007-08-21 17:44
醉卧千山
Rank: 1
等 级:新手上路
帖 子:100
专家分:0
注 册:2007-4-15
收藏
得分:0 
<generator class="native" />改为
<generator class="assigned" />试试看呢
这样应该不会自动增加了

天无绝人之路!
2007-08-21 21:46
susan001983
Rank: 1
等 级:新手上路
威 望:1
帖 子:125
专家分:0
注 册:2007-7-25
收藏
得分:0 
根据我的经验,自动增长列的"?"可以不用加,加了会报错.


2007-08-22 09:45
huluhulu88
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-5-19
收藏
得分:0 
我说老兄,你指的是哪里的“?”啊,你不会指的是这条语句吧,这是hibernate自动产生的。[CODE]insert into KnowledgeSystem.dbo.kms_comments (UserId, CommentsType, FindId, Answer, AnswerTime, commentsId) values (?, ?, ?, ?, ?, ?) select scope_identity()[/CODE]
我现在想问的是,我没有插入主键的值,可是hibernate为什么会帮我加,害得我出错。

2007-08-22 10:10
支离破碎
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:737
专家分:0
注 册:2007-1-4
收藏
得分:0 
你是关联表?试下先加另一张表的数据,.可能是由于另一个表数据没的所以ID没产生,这个表又要用所以出错找不到ID.

人生漂泊無依,有如浮萍菱花,隨水飄流,你会在我这里停留吗?
[url=http://51mynet.]http://51mynet.[/url]
2007-08-22 22:02
sea7
Rank: 2
等 级:新手上路
威 望:3
帖 子:291
专家分:0
注 册:2007-2-17
收藏
得分:0 

试试3楼的方法


软件是一种修养
2007-08-25 10:02
神经塔
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1987
专家分:4
注 册:2006-7-1
收藏
得分:0 

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


[shadow=255,red,2]待到秋来九月八,我花开时百花杀。冲天香阵透长安,满城尽是黄金甲。 [/shadow]
2007-08-27 17:03
快速回复:hibernate 执行插入时报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018769 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved