Hibernate级联删除提示约束失败DELETE 语句与 REFERENCE 约束"FK_SelectCourse_StudentInfo"冲突。

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

前台执行删除带约束关系的时候,后台提示错误


 
 
  1. Hibernate: delete from Student.dbo.StudentInfo where Snu=?  
  2. 删除失败!  
  3. org.hibernate.exception.ConstraintViolationException: could not delete: [com.yaxing.domain.StudentInfo#1               ]  
  4.     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)  
  5.     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)  
  6.     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2710)  
  7.     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2893)  
  8.     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)  
  9.     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)  
  10.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)  
  11.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189)  
  12.     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)  
  13.     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)  
  14.     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)  
  15.     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)  
  16.     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)  
  17.     at com.yaxing.dao.StudentInfoDAO.delete(StudentInfoDAO.java:79)  
  18.     at com.yaxing.service.AdminServerImpl.deleteStudent(AdminServerImpl.java:23)  
  19.     at com.yaxing.admin.action.StudentInfoAction.deleteStudent(StudentInfoAction.java:237)  
  20.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  21.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  22.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  23.     at java.lang.reflect.Method.invoke(Method.java:597)  
  24.     at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)  
  25.     at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)  
  26.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)  
  27.     at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)  
  28.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  29.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  30.     at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)  
  31.     at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)  
  32.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  33.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  34.     at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)  
  35.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  36.     at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)  
  37.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  38.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  39.     at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)  
  40.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  41.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  42.     at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)  
  43.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  44.     at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)  
  45.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  46.     at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)  
  47.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  48.     at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)  
  49.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  50.     at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)  
  51.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  52.     at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)  
  53.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  54.     at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)  
  55.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  56.     at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)  
  57.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  58.     at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)  
  59.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  60.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  61.     at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)  
  62.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  63.     at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)  
  64.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  65.     at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)  
  66.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  67.     at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)  
  68.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)  
  69.     at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)  
  70.     at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)  
  71.     at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)  
  72.     at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)  
  73.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)  
  74.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
  75.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
  76.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
  77.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)  
  78.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
  79.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
  80.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)  
  81.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)  
  82.     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)  
  83.     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)  
  84.     at java.lang.Thread.run(Thread.java:619)  
  85. Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: DELETE 语句与 REFERENCE 约束"FK_SelectCourse_StudentInfo"冲突。该冲突发生于数据库"Student",表"dbo.SelectCourse", column 'Snu'。  
  86.     at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)  
  87.     at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)  
  88.     at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)  
  89.     at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteUpdate(Unknown Source)  
  90.     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)  
  91.     at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)  
  92.     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2689)  
  93.     ... 78 more  
  94.  

数据库的关联关系如下:

 

StudentInfo 表snu 为主键

SelectCourse表 中 snu 参照StudentInfo 表 中snu,直接删除

Hibernate 配置文件已设置级联删除。由于Hibernate配置文件是根据Sql Server 2005 数据库反向生成修改而来的。

前台直接删除带约束关系的记录的时候,报错。错误如上图所示!

解决办法:设置Sql Server 2005 的级联删除:具体操作如图所示:

1.选择关系:打开约束。

 

2.找到INSERT 和UPDATE 规范:默认的更新规则 和删除规则为无操作!

 

3.修改更新规则和删除规则为层叠。

 

4.保存.

5.删除成功!






 本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/577449,如需转载请自行联系原作者


相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
Java 数据库连接
【hibernate validator】(五)分组约束
【hibernate validator】(五)分组约束
|
Java 数据库连接
hibernate validator】(三)声明和验证方法约束
hibernate validator】(三)声明和验证方法约束
|
安全 Java 数据库连接
【hibernate validator】(四)内插约束错误消息
【hibernate validator】(四)内插约束错误消息
|
Java 数据库连接 API
【hibernate validator】(二)声明和验证Bean约束(下)
【hibernate validator】(二)声明和验证Bean约束(下)
|
Java 数据库连接 容器
【hibernate validator】(二)声明和验证Bean约束(上)
【hibernate validator】(二)声明和验证Bean约束
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
|
Java 数据库连接
【hibernate validator】(六)创建自定义约束
【hibernate validator】(六)创建自定义约束
|
Oracle 关系型数据库 Java
hibernate操作Oracle出现违反唯一约束条件
出现这个问题的原因无非是主键冲突,我这里引起主键冲突的原因是实体类使用的ID策略为
|
Java 数据库连接
hibernate 自连接 注解 item parentItem ( 非级联删除 , 级联查询)
  @JsonAutoDetect 解决 该bug: Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by t...
961 0