2015-09-08 66 views
1

我收到SQL,休眠(註釋):無法從表

java.sql.BatchUpdateException刪除父行:不能刪除或更新父行:一個 外鍵約束失敗

當試圖刪除一行。

看來我錯誤地映射了關係。

從頂部開始:

我不得不實體。電錶和電力閱讀。

因爲我只想知道哪個儀表每次讀取屬於我決定多對一單向關係,這是如下:

閱讀:

@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 is below。不幸的是,我用波蘭語寫了這個程序,上面的課程已經在旅途中翻譯過了。 出現在堆棧中的單詞:

odczyt-閱讀;

licznik米;

energia- electricity;

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.ALLElectricityReading意味着所有相關行動級聯閱讀米,沒有倒過來。所以如果你刪除一個儀表,不會發生級聯。

要解決這個問題,只需添加級聯的另一個方向,並將orphanRemoval = true屬性添加到註釋中。由於ElectricityReading仍然是關係的所有者,因此不需要對數據庫進行任何更改。

如果你不想這樣做,你必須先手動刪除相應的讀數。

+0

我會盡力而爲。謝謝。 – Maciej