SSH+Oracle问题:org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

在进行SSH整合开发过程中,自己遇到了一个问题,花了近一天时间才解决掉。

首先,页面报的错误为:

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

 

在控制台中,报错为:

java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:752) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) 

 

1.对于页面上的错误

原因:数据库表中设置了关联关系,但是在插入数据的时候没有设置关联关系,插入的时候就会报错
改正:设置对表之间的关联关系就可以了

 

2.控制台的错误

出错了,我们一般都是看控制台,但这个错误与真正的错误去无关,具体原因,可以GOOGLE  or BAIDU 一下

 

数据库中有一张表采用联合主键形式,其一一个键又是外键

 

具体原因还是出错还是在于问题1,在设计Oracle数据时候,我们对自动标识id同时设置了Trigger和Sequence。但对于hibernate来说,在插入数据之前会先运行一次Sequence生成id的,所以这时如果还有触发器(TRIGGER),又要再运行一次Sequence,于是就会发现id是以2为步进增加的。这时把Trigger删除,步进就为1了。

 

实体类的主键配置策略如下:

TEMP_VEHICLE_DATA_SEQS

 

下面为hibernate处理代码:

 getHibernateTemplate().setFlushMode(0); getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { session.connection().setAutoCommit(false); Set tempMtItem = list.get(0).getTempMtItems(); for (TempMtItem mt : tempMtItem) { session.save(mt); for (Object lan : mt.getTempMtItemLanguages()) { TempMtItemLanguage tempLan = (TempMtItemLanguage) lan; TempMtItemLanguageId id = new TempMtItemLanguageId(); id.setItemId(mt.getId()); id.setLngCde(tempLan.getLanguage()); tempLan.setId(id); session.save(tempLan); } } for (TempVehicleData temp : list) session.save(temp); session.flush(); session.connection().commit(); return null; } }); getHibernateTemplate().setFlushMode(1);

在调用session.save()或者getHibernateTemplate().save()方法时会自动生成根据sequence把查询到的id号set给这个对象的id
 

 附几篇文章链接:

http://www.blogjava.net/caixuetao/archive/2007/04/26/113433.html

http://www.cnblogs.com/xuwenyan/archive/2008/02/18/1072795.html

http://www.cppblog.com/jerryma/archive/2010/03/10/109334.aspx

http://coldandhot.blog.hexun.com/11958153_d.html

 

 


 

你可能感兴趣的:(SSH+Oracle问题:org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)