- 浏览: 10196 次
- 性别:
- 来自: 杭州
文章分类
最新评论
现有两个对象:Diary和DiaryReply
表结构如下:
现要删除Diary的时候级联删除,跟该Diary关联的DiaryReply
报如下错误:
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:575)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy13.deleteDiary(Unknown Source)
at cn.edu.zjut.software.swordye.service.DiaryServiceTest.testDeleteDiary(DiaryServiceTest.java:114)
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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.unitils.UnitilsJUnit4TestClassRunner$CustomTestMethodRunner.executeMethodBody(UnitilsJUnit4TestClassRunner.java:231)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.unitils.UnitilsJUnit4TestClassRunner$CustomTestClassMethodsRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:155)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:95)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`osblogdb_test/osblog_diary_reply`, CONSTRAINT `fk_r_diary_reply` FOREIGN KEY (`diary_id`) REFERENCES `osblog_diary` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 39 more
一开始总以为是one-to-many里cascade配置错误,但是尝试了各种配置后还是一样报这个错误。经过多翻网上查找终于在一篇文章里看了。原来是one-to-many端的<key column="id" />配置错误,因为*.hbm.xml文件是由我用HibernateSynchronizer生成的,一直就没有注意这个key值。该值应该是many端外键所在的属性列名,即:DiaryReply所对应表的diary_id。
看来这个HibernateSynchronizer工具有bug啊!害偶花了N多时间找这个错误。。以后用这个工具的时候要小心鸟。
表结构如下:
-- ---------------------------- -- Table structure for osblog_diary_reply -- ---------------------------- CREATE TABLE `osblog_diary_reply` ( `id` int(11) NOT NULL auto_increment, `diary_id` int(11) NOT NULL, `author` varchar(20) NOT NULL, `author_url` varchar(100) default NULL, `author_email` varchar(50) default NULL, `owner_only` int(11) default NULL, `content` text NOT NULL, `create_time` datetime NOT NULL, `last_modify_time` datetime default NULL, `last_update_time` timestamp NULL default NULL on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `r_diary_reply_fk` (`diary_id`), CONSTRAINT `fk_r_diary_reply` FOREIGN KEY (`diary_id`) REFERENCES `osblog_diary` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for osblog_diary -- ---------------------------- CREATE TABLE `osblog_diary` ( `id` int(11) NOT NULL auto_increment, `blog_id` varchar(32) NOT NULL, `catalog_id` int(11) NOT NULL COMMENT '日记分类', `author` varchar(20) default NULL, `title` varchar(200) NOT NULL, `content` text NOT NULL, `diary_size` int(11) NOT NULL, `view_count` int(11) NOT NULL, `reply_count` int(11) NOT NULL, `refer` varchar(100) default NULL COMMENT '引用地址', `weather` varchar(20) default NULL, `mood_level` smallint(6) default NULL COMMENT '心情指数', `tags` varchar(100) default NULL, `last_read_time` datetime default NULL, `last_reply_time` datetime default NULL, `reply_notify` smallint(6) default NULL COMMENT '回复是否通知', `locked` smallint(6) default NULL COMMENT '是否锁定(保留)', `create_time` datetime NOT NULL, `last_modify_time` datetime default NULL, `last_update_time` timestamp NULL default NULL on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `r_catalog_diary_fk` (`catalog_id`), KEY `r_blog_diary_fk` (`blog_id`), CONSTRAINT `fk_r_blog_diary` FOREIGN KEY (`blog_id`) REFERENCES `osblog_site_blog` (`blog_id`), CONSTRAINT `fk_r_catelog_diary` FOREIGN KEY (`catalog_id`) REFERENCES `osblog_diary_catalog` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*******diary.hbm.xml*******/ <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="cn.edu.zjut.software.swordye.model"> <class name="Diary" table="osblog_diary" > <meta attribute="sync-DAO">false</meta> <id name="id" type="integer" column="id" > <generator class="native"/> </id> <property name="author" column="author" type="string" not-null="false" length="20" /> <property name="title" column="title" type="string" not-null="true" length="200" /> <property name="content" column="content" type="string" not-null="true" /> <property name="diarySize" column="diary_size" type="integer" not-null="true" length="10" /> <property name="viewCount" column="view_count" type="integer" not-null="true" length="10" /> <property name="replyCount" column="reply_count" type="integer" not-null="true" length="10" /> <property name="refer" column="refer" type="string" not-null="false" length="100" /> <property name="weather" column="weather" type="string" not-null="false" length="20" /> <property name="moodLevel" column="mood_level" type="java.lang.Short" not-null="false" length="5" /> <property name="tags" column="tags" type="string" not-null="false" length="100" /> <property name="lastReadTime" column="last_read_time" type="timestamp" not-null="false" /> <property name="lastReplyTime" column="last_reply_time" type="timestamp" not-null="false" /> <property name="replyNotify" column="reply_notify" type="java.lang.Short" not-null="false" length="5" /> <property name="locked" column="locked" type="java.lang.Short" not-null="false" length="5" /> <property name="createTime" column="create_time" type="timestamp" not-null="true" /> <property name="lastModifyTime" column="last_modify_time" type="timestamp" not-null="false" /> <many-to-one name="blog" column="blog_id" class="SiteBlog" not-null="true" > </many-to-one> <many-to-one name="diaryCatalog" column="catalog_id" class="DiaryCatalog" not-null="true" > </many-to-one> <set name="diaryReplies" inverse="true" cascade="delete" lazy="true"> <key column="id"/> //这里配置是错误的,应该改为子表里的diary_id属性列 <one-to-many class="DiaryReply"/> </set> </class> </hibernate-mapping> ////*****diaryReply.hbm.xml******//// <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="cn.edu.zjut.software.swordye.model"> <class name="DiaryReply" table="osblog_diary_reply" > <meta attribute="sync-DAO">false</meta> <id name="id" type="integer" column="id" > <generator class="native"/> </id> <property name="blogId" column="blog_id" type="string" not-null="true" length="32" /> <property name="authorName" column="author" type="string" not-null="true" length="20" /> <property name="authorUrl" column="author_url" type="string" not-null="false" length="100" /> <property name="authorEmail" column="author_email" type="string" not-null="false" length="50" /> <property name="ownerOnly" column="owner_only" type="integer" not-null="false" length="10" /> <property name="content" column="content" type="string" not-null="true" /> <property name="createTime" column="create_time" type="timestamp" not-null="true" /> <property name="lastModifyTime" column="last_modify_time" type="timestamp" not-null="false" /> <many-to-one name="diary" column="diary_id" class="Diary" not-null="true" cascade="none" > </many-to-one> </class> </hibernate-mapping>
现要删除Diary的时候级联删除,跟该Diary关联的DiaryReply
public void deleteDiary(Diary diary) { getSession().delete(diary); }
报如下错误:
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:575)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy13.deleteDiary(Unknown Source)
at cn.edu.zjut.software.swordye.service.DiaryServiceTest.testDeleteDiary(DiaryServiceTest.java:114)
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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.unitils.UnitilsJUnit4TestClassRunner$CustomTestMethodRunner.executeMethodBody(UnitilsJUnit4TestClassRunner.java:231)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.unitils.UnitilsJUnit4TestClassRunner$CustomTestClassMethodsRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:155)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:95)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`osblogdb_test/osblog_diary_reply`, CONSTRAINT `fk_r_diary_reply` FOREIGN KEY (`diary_id`) REFERENCES `osblog_diary` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 39 more
一开始总以为是one-to-many里cascade配置错误,但是尝试了各种配置后还是一样报这个错误。经过多翻网上查找终于在一篇文章里看了。原来是one-to-many端的<key column="id" />配置错误,因为*.hbm.xml文件是由我用HibernateSynchronizer生成的,一直就没有注意这个key值。该值应该是many端外键所在的属性列名,即:DiaryReply所对应表的diary_id。
看来这个HibernateSynchronizer工具有bug啊!害偶花了N多时间找这个错误。。以后用这个工具的时候要小心鸟。
相关推荐
"Hibernate one-to-many / many-to-one关系映射"是两个基本的关系类型,用于表示实体间的关联。在这个主题中,我们将深入探讨这两种关系映射的原理、配置和实际应用。 一、一对一(one-to-one)与一对多(one-to-...
### Hibernate Many-to-One (多对一) 及 Cascade (级联) #### 一、Many-to-One(多对一) 在数据库设计中,实体之间的关系主要包括一对一、一对多、多对多等几种类型。而在ORM(对象关系映射)框架Hibernate中,...
在Hibernate中,`one-to-many`关系是常见的实体间关系之一,表示一个实体可以与多个其他实体相关联。本文将深入探讨`Hibernate one-to-many`注解的使用和实现细节。 ### 一、`@OneToMany`注解概述 `@OneToMany`...
本文主要关注Hibernate中的一个核心概念——一对一(One-to-One)、一对多(One-to-Many)和多对一(Many-to-One)关联映射,特别是关于“一到多”单向和双向关联映射的配置心得。 首先,让我们了解“一到多”关联...
标题"Hibernate one to many(many to one) 配置"涉及到的是关系型数据库在Java中的持久化框架Hibernate中的两种关联映射关系:一对一(One-to-One)和多对一(Many-to-One)。在数据库设计中,这种关系很常见,例如...
【标题】"hibernate-one-to-many-uni" 指的是在Hibernate框架下实现的一对多单向关联关系。Hibernate是Java开发中常用的持久化框架,它简化了数据库操作,使得开发者可以更加专注于业务逻辑而不是数据库交互。在这个...
<many-to-one name="department" class="com.example.Department" column="dept_id" /> ``` 接下来,我们将在Spring中配置Hibernate SessionFactory,并创建DAO接口及其实现。在添加部门时,可以同时添加员工到部门...
本文将深入探讨Hibernate5中的映射关系,主要包括多对多(Many-to-Many)和一对多(One-to-Many)这两种关系。 一、多对多映射(Many-to-Many) 多对多关系在数据库中表现为两个表之间存在多个连接记录,而在对象...
<many-to-one name="department" class="com.example.Department" column="DEPARTMENT_ID" /> ``` 如果使用注解,上述关系已经在实体类中定义。 ### 3. 操作一对多关系 在实际应用中,我们可以通过Hibernate提供...
10. **多对一(One-to-Many)、一对多(Many-to-One)、一对一(One-to-One)、多对多(Many-to-Many)关系**: Hibernate支持这四种数据库关联关系,通过不同的注解配置来实现。 **使用Hibernate的步骤** 1. **...
在给出的例子中,有四个表:A、AA、B和BB,它们之间存在一对多(one-to-many)和一对一(one-to-one)的关系。例如,A表与AA表是一对多关系,而A表与B表是一对一关系,B表与BB表也是一对多关系。 在Hibernate的映射...
<many-to-one name="childEntity" class="com.example.ChildEntity" cascade="all,delete-orphan" /> ``` 在这个例子中,父实体的`childEntity`属性将执行全部级联操作,包括添加、删除和更新,同时`delete-orphan`...
标题“hb-04-one-to-many-uni”和描述中的信息看似简洁,但结合标签“Java”,我们可以推测这是一个关于Java编程中的一个特定主题——一对一(One-to-One)、一对多(One-to-Many)关系的统一处理(uni)的示例项目...
在Java持久化框架Hibernate中,`one_to_many`映射是一种常见的关系模型,它表示一个实体(类)可以与多个其他实体(类)关联。在这个关系中,一个实体(通常是"one"端)对应多个实体实例("many"端)。在数据库中,...
本篇文章将全面探讨Hibernate的关联映射学习,包括一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)以及多对多(Many-to-Many)四种关系映射。 首先,一对一关联映射(One-to-One)是两个实体...
在Java的持久化框架Hibernate中,`One-to-Many`映射关系是对象关系映射(ORM)中常见的一种关联类型。它表示一个实体("One"端)与多个其他实体("Many"端)之间的关系。例如,一个学生可以有多个课程,一个老师可以...
《深入理解Hibernate:第三解——Many-to-One关系映射实战》 在Java世界里,ORM(Object-Relational Mapping)框架的出现极大地简化了数据库与Java对象之间的交互,而Hibernate作为其中的佼佼者,更是备受开发者青睐...
外键映射是通过在主表映射文件中使用`one-to-one`元素,而从表映射文件中使用`many-to-one`元素来实现。主键映射则是两个实体类的主键相互关联,这需要在`one-to-one`节点上设置`constrained`属性为`true`,以表明...
标题中的“Hibernate - one to many - update”指向的是一个关于Hibernate框架中的一对多关联关系在更新操作时的专题。Hibernate是Java开发中常用的持久化框架,它简化了数据库操作,使得开发者可以更加关注业务逻辑...