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

101 篇文章 1 订阅
86 篇文章 1 订阅

环境

Eclipse Indigo + Hibernate3.2 + Oracle 11g R2 + SQLPlus

 

问题

运行Hibernate程序,报如下异常

 

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
	at com.addbook.dao.impl.AdminDaoImpl.register(AdminDaoImpl.java:113)
	at com.addbook.service.impl.AdminServiceImpl.register(AdminServiceImpl.java:71)
	at com.addbook.dao.impl.TestAdmin.testRegisterByEntity(TestAdmin.java:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.BatchUpdateException: ORA-01400: 无法将 NULL 插入 ("WGB"."ADMIN"."CREATED_DATETIME")

	at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
	at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
	... 33 more


 

解决

 

出现这个问题原因是违反了约束。不能向不为空的列插入空值。查看映射文件,发现有如下属性的映射:

<property name="createdDatetime"></property>

再查询表结构,发现以前建的表多了个属性,于是解决办法就来了。

 

 

 

 

1.       将映射文件改成如下形式,指定列名:

 

<property name="createdDatetime" column="CREATED_DATETIME"></property>


 

 

2.       删除多余的列

 

alter table admin drop column CREATEDDATETIME;


 

 

3.       重新插入数据。

 

参考资料

 

http://zhidao.baidu.com/question/233479527.html

 

http://www.iteye.com/problems/10596

 

 

### 回答1: org.hibernate.exception.ConstraintViolationException: 无法执行语句。 这个异常通常是由于违反了数据库约束条件而引起的。可能是插入或更新数据时,数据不符合数据库表的约束条件,例如唯一性约束、外键约束等。需要检查数据是否符合表的约束条件,并进行相应的修改。 ### 回答2: 在Hibernate的执行过程中,可能会出现一个叫做“org.hibernate.exception.constraintviolationexception: could not execute statement”的异常。这个异常一般是由数据库的约束条件导致的。 约束条件是数据库中很重要的一个概念,它可以保证数据的完整性和一致性。常见的约束条件有主键约束、唯一约束、非空约束、外键约束等。当我们在Hibernate中执行一个操作(如插入、更新、删除等)时,如果操作违反了数据库中的约束条件,就会出现上述异常。 例如,当我们向一个表中插入一条数据时,如果该表有一个唯一约束的字段,而我们插入的数据与该字段已有的数据重复,就会出现约束违规的异常。 解决这个异常的方法一般有以下几种: 1. 检查Hibernate的映射文件(即实体类和数据库表之间的映射关系),确认约束条件是否设置正确。 2. 检查数据库中的约束条件是否正确设置,可以根据异常中的提示信息来确定是哪个约束条件出了问题。 3. 手动执行一些操作,如删除重复数据等,以使数据与约束条件相符。 4. 使用try-catch语句来捕获异常,详细处理约束的相关信息。 总之,在开发过程中,遇到这种异常时,需要仔细分析异常的来源,并通过逐一排查问题的方法解决异常。 ### 回答3: 该错误是因为在向数据库中进行写操作时,违反了数据库表中某个字段的约束条件,从而导致操作失败。常见的约束条件包括唯一性、非空性、外键约束等等。比如在插入一条新数据时,如果这个数据已经存在于表中,就会触发唯一性约束,从而导致该错误的出现。 解决这个错误的方法主要有以下几个方面: 1.检查数据表的约束条件是否正确设置,特别是在数据表的设计过程中可能会偶然地设置重复的约束条件。 2.查看具体的异常信息,根据异常信息找到出问题的具体语句和表,尽可能地排除错误可能性。 3.在代码中加入更加详细的异常判断,例如捕捉并打印出根本原因,这样有助于更快地解决问题。 4.在新数据插入之前,先查询数据库表中是否已经存在相同的数据记录,如果已经存在,则不要进行插入操作,从而避免出现唯一性约束的错误。 5.增加代码中的日志输出来记录发生错误的时间和上下文信息,对于定位问题有很大的帮助。 总之,这种错误通常有一个具体的原因,要细心检查代码和数据表,从而尽可能地找到并解决它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值