Dani G I'm also getting a similar issue with Hibernate 4.3.6.Final.
The only workaround is I can set the index manually (i.e. using comment.setIndex( post.getComments().size() ); ), but if I leave it as default then Hibernate will make the child/children's position stay at `0`.
In effect:
-
Original list: 0, 1, 2, 3
-
List with added children: 0, 1, 2, 3, 0, 0, 0
I tried Marc Schipperheyn's method but in my case it doesn't work and I get:
java.lang.IllegalStateException: Multiple representations of the same entity [id.co.bippo.cart.jpa.Cart#3f62a32c-ed1c-4ddd-84b2-1b76228e82dc] are being merged. Managed: [Cart [id: 3f62a32c-ed1c-4ddd-84b2-1b76228e82dc] [formalId: 100000002] [status: open] [creationTime: 2014-10-17T10:24:59.676+07:00] [modificationTime: 2014-10-17T10:24:59.676+07:00] [shoppingActionTime: 2014-10-17T10:24:59.676+07:00] [shoppingExpiryTime: 2014-10-17T10:29:59.676+07:00] [stockReservationId: 623fe92e-ac4a-4d92-a107-6ceedd80976f] [currency: IDR] [paymentMethod: null] [shipmentType: jne_reg] [shippingDescription: null]]; Detached: [Cart [id: 3f62a32c-ed1c-4ddd-84b2-1b76228e82dc] [formalId: 100000002] [status: open] [creationTime: 2014-10-17T10:24:59.676+07:00] [modificationTime: 2014-10-17T10:24:59.676+07:00] [shoppingActionTime: 2014-10-17T10:24:59.676+07:00] [shoppingExpiryTime: 2014-10-17T10:29:59.676+07:00] [stockReservationId: 623fe92e-ac4a-4d92-a107-6ceedd80976f] [currency: IDR] [paymentMethod: null] [shipmentType: jne_reg] [shippingDescription: null]]
at org.hibernate.event.internal.EntityCopyNotAllowedObserver.entityCopyDetected(EntityCopyNotAllowedObserver.java:51)
at org.hibernate.event.internal.MergeContext.put(MergeContext.java:262)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:216)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:192)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:886)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868)
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
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.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:474)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:218)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:192)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:886)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868)
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
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.cascadeCollectionElements(Cascade.java:379)
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296)
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.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:474)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:343)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:886)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868)
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
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.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:248)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:189)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:858)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:289)
at com.sun.proxy.$Proxy117.merge(Unknown Source)
at id.co.bippo.cart.JpaCartRepositoryImpl.saveLine(JpaCartRepositoryImpl.java:192)
|