Hibernate已删除的对象将被重新保存

时间:2015-09-04 07:01:23

标签: java hibernate exception

它让我发疯了! 我知道这个错误信息意味着什么,但它不起作用!

我需要删除一个有一些关联的Hibernate实体。

如果我关闭我的应用程序并重新打开它,删除就可以了。但每次我创建该对象并删除后,我都会收到此错误。

似乎我的会话在某个我不在范围内的地方持有该对象。

继承我的删除代码段

                initialGroups.removeAll(masterGroups);
                for (InterfaceMasterSignalGroup group : initialGroups) {

                    group.getMasterSignals().forEach(s -> s.setMaster(null));
                    group.setMasterSignals(null);

                    group.getConfig().getMasterSignals().remove(group);
                    group.setConfig(null);

                    session.delete(group);

                }

首先,我删除2个引用部分并删除对象本身。 调用提交抛出"对象应该由级联"

重新保存

以下是映射,所有其他字段都没有关联

@OneToMany(fetch = FetchType.LAZY, mappedBy = InterfaceDestinationSignal.MASTER)
@Cascade({ CascadeType.SAVE_UPDATE, CascadeType.DETACH })
@XmlTransient
public List<InterfaceDestinationSignal> getMasterSignals() {
    return masterSignals;
}

public void setMasterSignals(List<InterfaceDestinationSignal> masterSignals) {
    firePropertyChange(InterfaceMasterSignalGroup.MASTER_SIGNALS, this.masterSignals, this.masterSignals = masterSignals);
}

@ManyToOne
@JoinColumn(name = InterfaceMasterSignalGroup.CONFIG_ID)
@Cascade({ CascadeType.DETACH })
@XmlTransient
public GenericConfigInterfaceEntity getConfig() {
    return config;
}

public void setConfig(GenericConfigInterfaceEntity config) {
    firePropertyChange(InterfaceMasterSignalGroup.CONFIG, this.config, this.config = config);
}

我有什么问题吗?

祝你好运, 帕斯卡

这是堆栈跟踪

但它没有帮助。它说我应该从SOME(不是哪个)引用对象中删除目标对象:(

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [x.y.z.interfaceentity.InterfaceMasterSignalGroup#1]
    at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1236)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
    at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
    at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:167)
    at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:158)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at x.y.z.database.DatabaseManager.commit(DatabaseManager.java:187)
    at x.y.z.interfacedatabase.InterfaceDatabaseManager.commit(InterfaceDatabaseManager.java:164)
    at x.y.z.view.dialog.InterfaceMasterDialog$1.widgetSelected(InterfaceMasterDialog.java:163)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:832)
    at org.eclipse.jface.window.Window.open(Window.java:808)
    at x.y.z.view.dialog.InterfaceMasterDialog.initAndOpen(InterfaceMasterDialog.java:101)
    at x.y.z.view.dialog.InterfaceEditDialog$2.widgetSelected(InterfaceEditDialog.java:214)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:832)
    at org.eclipse.jface.window.Window.open(Window.java:808)
    at x.y.z.view.dialog.InterfaceEditDialog.initAndOpen(InterfaceEditDialog.java:122)
    at x.y.z.view.dialog.InterfaceDialog$2.mouseDoubleClick(InterfaceDialog.java:263)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:196)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:832)
    at org.eclipse.jface.window.Window.open(Window.java:808)
    at x.y.z.view.dialog.InterfaceDialog.open(InterfaceDialog.java:134)
    at x.y.z.view.dialog.InterfaceDialog.initAndOpen(InterfaceDialog.java:127)
    at x.y.z.handler.ManageInterfaceHandler.execute(ManageInterfaceHandler.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:229)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
    at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:149)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
    at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
    at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:825)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:701)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$6(HandledContributionItem.java:685)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:613)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:164)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1438)

2 个答案:

答案 0 :(得分:1)

看来,您的对象group仍在其他地方引用。为了正确删除它,您必须从其引用的其他位置删除它。

例如:列表,地图或任何其他收藏品等。

检查它是否还在裁判任何其他地方。

答案 1 :(得分:0)

好的,我已经解决了这个问题。第8层问题坐在前面!

在分配期间,我已将组分配给destinationSignal,但反之亦然。缺少这导致组中的空集合(因此没有删除任何内容)并且异常发生。

重新打开应用程序从数据库中删除了正确的模型,删除正常。