Hello everyone,
I have a bidirectional one to many relationship between two entities Cluster and Wagon.
A Cluster may be associated with multiple wagons.
The code lookes like this:
Code:
public class Cluster {
@OneToMany(mappedBy = "associatedCluster",targetEntity = Wagen.class)
public Collection<Wagen> getAssociatedWagen() {
return this.associatedWagen;
}
}
public class Wagen {
@ManyToOne(targetEntity = Cluster.class)
@JoinColumn(name = "CLUSTER_ID")
public Cluster getAssociatedCluster() {
return associatedCluster;
}
}
So with this structure, I am trying to insert/update data through a JUnit test.
Code:
private void persistTestData(Cluster kc) {
em.merge(kc);
for(Wagen w: kc.getAssociatedWagen()) {
em.merge(w);
}
persistedTestData.add(kc);
}
and I get the exception:
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: de.db.ewds.common.datamodel.entities.Wagen
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:611)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:244)
at de.db.ewds.stammdaten.kleinstecluster.service.KleinstesClusterServiceTest.persistTestData(KleinstesClusterServiceTest.java:96)
at de.db.ewds.stammdaten.kleinstecluster.service.KleinstesClusterServiceTest.testFindById(KleinstesClusterServiceTest.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: de.db.ewds.common.datamodel.entities.Wagen
at org.hibernate.event.def.DefaultMergeEventListener.getTransientCopyCache(DefaultMergeEventListener.java:128)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:90)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:706)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:690)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:694)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235)
at de.db.ewds.stammdaten.kleinstecluster.service.KleinstesClusterServiceTest.persistTestData(KleinstesClusterServiceTest.java:96)
at de.db.ewds.stammdaten.kleinstecluster.service.KleinstesClusterServiceTest.testFindById(KleinstesClusterServiceTest.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator.executeTest(BlockJUnit4ClassRunnerDecorator.java:126)
at mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator.invokeExplosively(BlockJUnit4ClassRunnerDecorator.java:104)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
... 6 more
Whatever I try to merge, it complains for the other entity.
Do you have any ideas?