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

问题描述

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关系映射:

user实体:

user关系映射:

删除用户的JSP页面:

删除用户的action方法:

删除用户的Service层:

删除用户的Dao层:

删除用户的struts2配置:

删除用户的spring配置(action):

解决方案

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

解决方案二:

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

解决方案三:

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

解决方案四:

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

解决方案五:

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

解决方案六:

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

解决方案七:

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

时间: 2024-08-03 09:15:30

spring-SSH项目删除用户时出错Cannot delete or update的相关文章

centos-openstack创建管理员用户时出错

问题描述 openstack创建管理员用户时出错 我用的是centos 6.5 在控制节点装openstack icehouse 但是到了创建管理员用户时出现了如题的错误 即an unexpected error prevented the server from fulfilling your request( http 500), 而网上的办法 export OS_SERVICE_TOKEN=570f150cb897e793e58f export OS_SERVICE_ENDPOINT=ht

MSSQL中删除用户时数据库主体在该数据库存中拥有架构 无法删除的解决方法_MsSql

在ms sql2005 下面删除一个数据库的用户的时候提示 "数据库主体在该数据库中拥有架构,无法删除" 的错误解决方案 1.在 安全性 -> 架构 下面看有没有该用户存在,如果有就删除 再试试在用户下面看能不能把该用户删掉,如果不行就用下面的文法 运行下SQL语句 ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo; --然后手动删除就可以了. 因为选定的用户拥有对象,所以无法除去该用户"解决方法 复制代码 代码如下: use

visual studio 2013开发web项目打开网页时出错

问题描述 可以在任务栏看到IISexpress图标,我用的是win7,iisexpress8.0上网查原因,于是他们说要打开控制面板中打开和关闭windows功能中的iis服务打开控制面板,然后发现打开和关闭windows功能一片空白用了注册表的方法等都没用我的windows系统却无法更新也无法安装windowsupdate独立安装包好像什么都不行我真的不知道怎么办了,反正现在我无法开发任asp.net程序了 解决方案 解决方案二:你的系统是属于"阉割版"的,还是安装一个靠谱的&quo

userdel 命令 - 从Linux系统中删除用户账户

在服务器上维护用户就是添加.修改以及删除用户.当一个用户出于某种原因不再需要登录系统时,我们需要删除此用户以避免安全漏洞.在Linux系统上,我们用 userdel 命令来删除一个用户. userdel是什么 userdel 是一个底层用于删除用户的工具.在 Debian 上,我们通常会使用 deluser 命令.userdel 会查询系统账户文件,例如 /etc/password 和 /etc/group.那么它会删除所有和用户名相关的条目.在我们删除它之前,用户名必须存在. 如何使用user

oracle怎么删除用户提供解决方案_oracle

在Oracle中删除用户时提示:ORACLE无法删除当前连接用户可以用以下语句 Sql代码 SQL> 复制代码 代码如下: select username,sid,serial# from v$session; USERNAME SID SERIAL# ------------------------------ ---------- ---------- 1 1 2 1 3 1 4 1 5 1 6 1 7 1 SYS 8 3 A 9 4 已选择9行. SQL> 复制代码 代码如下: alte

servlet-spring mvc项目访问jsp页面出错

问题描述 spring mvc项目访问jsp页面出错 严重: Servlet.service() for servlet jsp threw exception java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at org.gjt.xpp.impl.tokenizer.Tokenizer.next(Tokenizer.java:1274) at org.gjt.xpp.im

update-这是一个oracle计税公式,运行时出错missing right parenthesis

问题描述 这是一个oracle计税公式,运行时出错missing right parenthesis update 表名 t set 扣税= case (t.计税工资) when ((t.计税工资 <= 1500) and (t.计税工资 > 0)) then t.计税工资*0.03 when ((t.计税工资 <= 4500) and (t.计税工资 > 1500)) then t.计税工资*0.1-105 when ((t.计税工资 <= 9000) and (t.计税工

使用MongoDB3.2.6 64位update时出错

问题描述 使用MongoDB3.2.6 64位update时出错 如图 ,使用update更新时出错 解决方案 db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 你用双引号试试 解决方案二: update时使用子查询出错

spring-急!ssh项目出错Error setting value [[Ljava.lang.String;@1597a48]

问题描述 急!ssh项目出错Error setting value [[Ljava.lang.String;@1597a48] spring:4.1.5 struts2:2.3.20 hibernate:4.3.8 spring和struts已配置验证码的actionTomcat启动后没错误,进入到注册页面也没错误,点四位验证码更换时(如图)myeclipse的Console报如下错误: WARN OgnlValueStack:68 - Error setting value [[Ljava.l