2 mdd2012 mdd2012 于 2015.06.05 17:14 提问

SSH项目删除用户时出错Cannot delete or update

struts2:2.3.20

spring:4.1.5

hibernate:4.3.8

用户与订单一对多的关系,数据库中订单表有用户这个外键。在用户映射文件中已设置cascade="delete"

项目启动无错误,在后台管理用户页面删除用户时报如下错误:

16:35:30,957 WARN SqlExceptionHelper:144 - SQL Error: 1451, SQLState: 23000
16:35:30,959 ERROR SqlExceptionHelper:146 - Cannot delete or update a parent row: a foreign key constraint fails (mall.orderitem, CONSTRAINT FK_hth7ctygtqa5ov7kutghwa1ni FOREIGN KEY (uid) REFERENCES orders (oid))
16:35:30,961 INFO AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements
16:35:31,165 ERROR DefaultDispatcherErrorHandler:42 - Exception occurred during processing request: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:163)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:730)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:592)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.lonini.mall.user.service.UserService$$EnhancerBySpringCGLIB$$3596b2ab.deleteUser()
at com.lonini.mall.user.adminaction.UserAdminAction.delete(UserAdminAction.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
......

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)

......

Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (mall.orderitem, CONSTRAINT FK_hth7ctygtqa5ov7kutghwa1ni FOREIGN KEY (uid) REFERENCES orders (oid))
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
......

order实体:

order实体
order关系映射:
order关系映射
user实体:
user实体
user关系映射:
user关系映射
删除用户的JSP页面:
删除用户的JSP页面
删除用户的action方法:
删除用户的action方法
删除用户的Service层:
删除用户的Service层
删除用户的Dao层:
删除用户的Dao层
删除用户的struts2配置:
删除用户的struts2配置
删除用户的spring配置(action):
删除用户的spring配置(action)

7个回答

jiuqiyuliang
jiuqiyuliang   2015.06.05 20:24

配置的一对多的关系配置的不对把:http://blog.csdn.net/jiuqiyuliang/article/details/41015641

frank_20080215
frank_20080215   2015.06.06 10:12

级联删除,要么去掉这配置。
要么修改代码,先删除相应的订单,然后再删除用户,用事务

zhupt001
zhupt001   2015.06.06 12:20

你不要在action层获取用户,在service层查询获取这个用户,然后再使用hibernate自带的delete方法删除这个实体

yllcxsj
yllcxsj   2015.06.08 09:45

1.user在有些数据库里是关键字,不要设成表名,这也可能引起异常。2.查看异常,貌似是数据库里存在帐数据。可以清空后,再做测试

dengsong_01
dengsong_01   2015.06.08 14:58

同问同问,希望得到大神的帮助

sumcul
sumcul   2015.06.09 10:21

级联删除吧,也就是把外键依赖的表数据也同时删除。

datou431
datou431   Rxr 2015.06.10 15:28

级联删除,要么去掉这配置。

Csdn user default icon
上传中...
上传图片
插入图片