关于Hibernate 双向一对多的 级联 删除和增加问题
Rpt_dy_report(one)和 Rpt_dy_outputcol (many) 两个实体类 是一对多的双向关系
增加记录的时候 save(Rpt_dy_report) 能够成功的同时增加a和b 的记录
删除记录的时候 delete(Rpt_dy_report) 也能够成功的同时删掉a和b的记录
现在问题是 当执行完删除操作 在执行增加记录的时候 save(Rpt_dy_report)报错!!!
现在给出报错代码
ps: 不太明白为什么insert语句执行完后还要执行 update语句
Hibernate: select rpt_dy_rep0_.report_id as report1_9_, rpt_dy_rep0_.createTime as createTi2_9_, rpt_dy_rep0_.createUserName as createUs3_9_, rpt_dy_rep0_.operTime as operTime4_9_, rpt_dy_rep0_.operUserName as operUser5_9_, rpt_dy_rep0_.hotel_id as hotel10_9_, rpt_dy_rep0_.query_type as query6_9_, rpt_dy_rep0_.querysql as querysql7_9_, rpt_dy_rep0_.report_name as report8_9_, rpt_dy_rep0_.report_title as report9_9_ from Rpt_dy_report rpt_dy_rep0_ where 1=1 and rpt_dy_rep0_.report_name=?
Hibernate: insert into Rpt_dy_report (createTime, createUserName, operTime, operUserName, hotel_id, query_type, querysql, report_name, report_title) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_report set createTime=?, createUserName=?, operTime=?, operUserName=?, hotel_id=?, query_type=?, querysql=?, report_name=?, report_title=? where report_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:181)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.hanb.hotel.service.report.ReportServiceImpl$$EnhancerByCGLIB$$d125196b.addDy_report(<generated>)
at com.hanb.hotel.action.report.ReportAction.addDy_report(ReportAction.java:172)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
增加记录的时候 save(Rpt_dy_report) 能够成功的同时增加a和b 的记录
删除记录的时候 delete(Rpt_dy_report) 也能够成功的同时删掉a和b的记录
现在问题是 当执行完删除操作 在执行增加记录的时候 save(Rpt_dy_report)报错!!!
现在给出报错代码
ps: 不太明白为什么insert语句执行完后还要执行 update语句
Hibernate: select rpt_dy_rep0_.report_id as report1_9_, rpt_dy_rep0_.createTime as createTi2_9_, rpt_dy_rep0_.createUserName as createUs3_9_, rpt_dy_rep0_.operTime as operTime4_9_, rpt_dy_rep0_.operUserName as operUser5_9_, rpt_dy_rep0_.hotel_id as hotel10_9_, rpt_dy_rep0_.query_type as query6_9_, rpt_dy_rep0_.querysql as querysql7_9_, rpt_dy_rep0_.report_name as report8_9_, rpt_dy_rep0_.report_title as report9_9_ from Rpt_dy_report rpt_dy_rep0_ where 1=1 and rpt_dy_rep0_.report_name=?
Hibernate: insert into Rpt_dy_report (createTime, createUserName, operTime, operUserName, hotel_id, query_type, querysql, report_name, report_title) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Rpt_dy_outputcol (createTime, createUserName, operTime, operUserName, col_attributename, col_datatype, col_realname, display_status, display_width, isGroup, isOrder, isquery, isstatistics, orderType, output_order, query_con, report_id, statistics_group, statistics_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_report set createTime=?, createUserName=?, operTime=?, operUserName=?, hotel_id=?, query_type=?, querysql=?, report_name=?, report_title=? where report_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
Hibernate: update Rpt_dy_outputcol set createTime=?, createUserName=?, operTime=?, operUserName=?, col_attributename=?, col_datatype=?, col_realname=?, display_status=?, display_width=?, isGroup=?, isOrder=?, isquery=?, isstatistics=?, orderType=?, output_order=?, query_con=?, report_id=?, statistics_group=?, statistics_type=? where outputcol_id=?
org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:181)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.hanb.hotel.service.report.ReportServiceImpl$$EnhancerByCGLIB$$d125196b.addDy_report(<generated>)
at com.hanb.hotel.action.report.ReportAction.addDy_report(ReportAction.java:172)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
...全文
相关推荐
Hibernate双向关联代码示例
**级联操作(Cascade)** 在双向关联中,级联操作允许我们通过一个对象的操作影响另一个对象。例如,`CascadeType.ALL`表示在删除或保存父对象时,也会相应地处理所有子对象。7.
Hibernate 一对多双向映射及乐观锁使用
**实战应用**:在实际开发中,可能会遇到的问题包括懒加载与立即加载的选择、集合的级联操作(如级联保存、删除)、缓存策略的设置等。7.
hibernate ppt
同时,通过 `cascade` 属性可以控制级联操作,例如,当删除一个区县时,所有关联的街道也会被删除。为了更好地理解这些关联,让我们看一个双向一对多的例子。
Hibernate映射关联详解
例如,可能需要考虑级联保存、级联删除等特性,以及如何处理更新时的数据一致性问题等。此外,随着业务复杂度的增加,还可以考虑使用更高级的功能,如懒加载、缓存机制等,以优化系统的性能表现。
Hibernate笔试题及答案
- **解析**:在三层架构(表现层、业务逻辑层、数据访问层)中,数据访问层主要负责与数据库的交互,包括数据的查询、增加、删除和修改等操作。


Web 开发
