问题相关对象:
DirectoryEntity
LinkEntity
LogEntity
问题相关关系:
DirectoryEntity与LinkEntity一对多
@OneToMany(fetch = FetchType.EAGER, mappedBy = "directoryByDirectoryId", cascade={CascadeType.REMOVE})
public Collection<LinkEntity> getLinksById() { return linksById; }
public void setLinksById(Collection<LinkEntity> linksById) {
this.linksById = linksById;
}
LinkEntity与LogEntity一对多
@OneToMany(fetch = FetchType.EAGER, mappedBy = "linkByLinkId", cascade={CascadeType.REMOVE})
public Collection<LogEntity> getLogsById() {
return logsById;
}
public void setLogsById(Collection<LogEntity> logsById) {
this.logsById = logsById;
}
发生异常的操作:
directoryRepository.delete(directoryEntity);
删除DirectoryEntity,能自动级联删除LinkEntity,但级联删除LinkEntity提示外键冲突:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`filexplorer`.`log`, CONSTRAINT `FKqdhv57gaa27apvae3v3i1rvdi` FOREIGN KEY (`link_id`) REFERENCES `link` (`id`))
即在删除LinkEntity时,不能级联删除LogEntity。
疑点:
LinkEntity的OneToMany配置与DirectoryEntity无异,为什么在删除LinkEntity时不能级联删除LogEntity?
详细Log:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`filexplorer`.`log`, CONSTRAINT `FKqdhv57gaa27apvae3v3i1rvdi` FOREIGN KEY (`link_id`) REFERENCES `link` (`id`))
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:422)
com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
com.mysql.jdbc.Util.getInstance(Util.java:387)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:934)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3233)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3470)
org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:98)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
com.jianyujianyu.service.DirectoryService$$EnhancerBySpringCGLIB$$3dcbb80b.deleteDirectory(<generated>)
com.jianyujianyu.controller.DirectoryController.directoryDelete(DirectoryController.java:133)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:891)
javax.servlet.http.HttpServlet.service(HttpServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:74)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
次日新发现:直接删除位于中间的Link也不行,因此是两层的级联删除也无法实现。请问如何实现级联删除,我已然在代码的注解中添加了CascadeType了。
认证高级PHP讲师