我正在接受
java.sql.BatchUpdateException:无法删除或更新父行:a 外键约束失败
尝试删除行时。
似乎我错误地映射了关系。
从顶部开始:
我必须要实体。电表和电力读数。
由于我只想知道每个读数属于哪个表,我决定使用ManyToOne单向关系,如下所示:
读:
@Entity
@Table(name = "ELECTRICITY_READING")
public class ElectricityReading {
@Id
@GeneratedValue
@Column
private int id;
@ManyToOne(cascade={CascadeType.ALL})
ElectricityMeter meter;
//some other columns
public ElectricityMeter getMeter() {
return meter;
}
public void setMeter(ElectricityMeter meter) {
this.meter = meter;
}
//other getters setters etc.
仪:
@Entity
@Table(name = "ELECTRICITY_METER")
public class ElectricityMeter extends {
@Id
@GeneratedValue
@Column
private int id;
@Column(nullable = false, unique = true)
private String serialNumber;
//some other columns
//getters setters etc.
据我所知,“@ ManyToOne(cascade = {CascadeType.ALL})”应该这样做,并允许我删除父行(米)并触发删除所有读数属于范围内的仪表,但它不会发生。 它反而给了我错误。
我应该如何正确映射实体,以便我能够删除父行以及同时删除孤立的行?
Stack在下面。不幸的是,我用波兰语写了这个程序,上面的课程已经在旅途中翻译过了。 堆栈中出现的单词:
odczyt-阅读;
licznik-meter;
能源电力; exception
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2484)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2702)
org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:77)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
kamienica.dao.LicznikDaoImpl.deleteLicznik(LicznikDaoImpl.java:68)
kamienica.service.LicznikServiceImpl.deleteLicznik(LicznikServiceImpl.java:58)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
com.sun.proxy.$Proxy29.deleteLicznik(Unknown Source)
kamienica.controller.LicznikController.usunLicznikEnergia(LicznikController.java:121)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
java.sql.BatchUpdateException: Cannot delete or update a parent row: a
foreign key constraint fails (`kamienica`.`odczyt_energia`, CONSTRAINT
`FK55CB21B13D42C17B` FOREIGN KEY (`licznik_licznik_id`) REFERENCES
`licznik_energia` (`licznik_id`))
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2484)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2702)
org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:77)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
kamienica.dao.LicznikDaoImpl.deleteLicznik(LicznikDaoImpl.java:68)
kamienica.service.LicznikServiceImpl.deleteLicznik(LicznikServiceImpl.java:58)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
com.sun.proxy.$Proxy29.deleteLicznik(Unknown Source)
kamienica.controller.LicznikController.usunLicznikEnergia(LicznikController.java:121)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
感谢您的帮助, 马切伊
答案 0 :(得分:1)
CascadeType.ALL
上的 ElectricityReading
表示所有相关操作都是从读取到电表,而不是相反。因此,如果删除仪表,则不会发生级联。
要解决此问题,只需添加其他方向并添加级联,并将orphanRemoval = true
属性添加到注释中。由于ElectricityReading
仍然是关系的所有者,因此不需要对数据库进行任何更改。
如果您不想这样做,则必须先手动删除相应的读数。