-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: On cascade error with entityManager
PostPosted: Sun Sep 23, 2012 8:53 pm 
Newbie

Joined: Thu Jan 05, 2012 6:42 am
Posts: 4
Hi guys!

i'm using Hubernate 4.1.7 Final with entity manager.. My entities are mapped correctly but when I try to do an insert an entity, during the cascade an error is generated ...

The stacktrace of the error is this:

Code:
Caused by: java.lang.IllegalStateException: An entity copy was already assigned to a different entity.
   at org.hibernate.event.internal.EventCache.put(EventCache.java:184)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:910)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
   at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
   at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
   at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:910)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
   at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
   at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:409)
   at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:350)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
   at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:910)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
   at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
   at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
   at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:423)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:213)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:884)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)


The same error was reported in the forum topic stackoverflow (link: http://stackoverflow.com/questions/10550511/illegalstateexception-with-hibernate-4-and-manytoone-cascading), but the solution presented to check the implementation of equals() and hashCode() has already been made and the error still persists!

debugging the code, I realized that the insides of the lists contained entities linked via 1:N are apparently being mistaken for the entity to be persisted ...

follows my implementation of equals and hashcode:

Code:
    @Override
    public int hashCode() {
       
        final int prime = 11;
       
        int result = 1;
        result = prime * result + getCode();
       
        return result;
    }

    @Override
    public boolean equals(Object object) {
       
        if (object == null) {
            return false;
        }
       
        if (getClass() != object.getClass()) {
            return false;
        }
       
        if (!(object.getClass().isAssignableFrom(Bill.class))) {
            return false;
        }
       
        final Bill other = (Bill) object;
       
        if (!other.isSaved()) {
            return false;
        }
       
        if (this.getCode() != other.getCode()) {
            return false;
        }
       
        return true;
    }


the error does not happen if I use hibernate 3.6 .. That would be a bug?


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Tue Sep 25, 2012 12:01 pm 
Newbie

Joined: Thu Jan 05, 2012 6:42 am
Posts: 4
anyone?


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Fri Jan 18, 2013 6:13 am 
Newbie

Joined: Fri Jan 18, 2013 6:08 am
Posts: 1
gregorioarthur wrote:
anyone?

the same question


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Tue Feb 19, 2013 7:40 pm 
Newbie

Joined: Tue Feb 19, 2013 7:17 pm
Posts: 1
I have the same issue. Also I find HHH-7605 defect (https://hibernate.onjira.com/browse/HHH-7605), but looks like that defect was not submitted correctly and no one want to fix them.

My debug log:
Code:
00:44:16,642 DEBUG ActionQueue:202 - Executing identity-insert immediately
00:44:16,642 DEBUG SqlStatementLogger:104 - insert into PRODUCT_SIZE_PRICE (PRICE, PRODUCT_ID, SIZE) values (?, ?, ?)
00:44:16,642 DEBUG PreparedStatementHandle:197 - insert into PRODUCT_SIZE_PRICE (PRICE, PRODUCT_ID, SIZE) values ('4613', 45861, '19.0мм 1.57гр')
00:44:16,643 DEBUG IdentifierGeneratorHelper:93 - Natively generated identity: 212748
00:44:16,643 DEBUG UpdateTimestampsCache:78 - Pre-invalidating space [PRODUCT_SIZE_PRICE], timestamp: 5575941802569728
00:44:16,643 DEBUG EhcacheGeneralDataRegion:100 - key: PRODUCT_SIZE_PRICE value: 5575941802569728
00:44:16,643 ERROR GenericDaoHibernate:80 - An entity copy was already assigned to a different entity.
java.lang.IllegalStateException: An entity copy was already assigned to a different entity.
   at org.hibernate.event.internal.EventCache.put(EventCache.java:184)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:910)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
   at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
   at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
   at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:884)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)
   at com.navigator.db.dao.impl.GenericDaoHibernate.saveOrUpdate(GenericDaoHibernate.java:78)
   at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at com.navigator.timer.GoldAffiliateCsvParser.parseAndSaveProductFromKupiZolotoLine(GoldAffiliateCsvParser.java:354)
   at com.navigator.timer.GoldAffiliateCsvParser.run(GoldAffiliateCsvParser.java:113)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)


I would to clarify that use "orphanRemoval=true" for this collection. And this error occurred only first time (during object creating - id is null at this place). When I updating existing object with same collection all works fine. Looks like that this is wrong hash/equals methods (or Ehcache bug???), but why this worked fine in 3.6? Any opinion?
--------------------------------------------------------------------------------------------------------------------------------------------------
After 6 days trying to fix this issue downgraded to 4.1.0.Final and looks like all works fine now.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Fri Mar 01, 2013 5:56 am 
Newbie

Joined: Fri Mar 01, 2013 5:53 am
Posts: 1
Same bug here; took way too long to realize that it wasn't my doing...

Downgrading to 4.1.1.Final solved it.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Thu Mar 07, 2013 3:53 pm 
Newbie

Joined: Tue Jan 18, 2011 3:24 pm
Posts: 13
I am facing the same error. Is there any other solution other than downgrading Hibernate?


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Mon Mar 11, 2013 1:34 pm 
Newbie

Joined: Mon Mar 11, 2013 1:31 pm
Posts: 1
HJLebbink wrote:
Same bug here; took way too long to realize that it wasn't my doing...

Downgrading to 4.1.1.Final solved it.


Same thing. Had a lot of headache lately trying to fix what I thought was my own issue. Downgrading helped though doesn't seem fine.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Tue Mar 12, 2013 2:23 am 
Beginner
Beginner

Joined: Mon Nov 26, 2012 4:08 am
Posts: 24
HJLebbink wrote:
Same bug here; took way too long to realize that it wasn't my doing...

Downgrading to 4.1.1.Final solved it.



We have the same problem. Unfortunately we can't downgrade because we are using Hibernate Search with spatial search which is dependant on a higher release of Hibernate. A fix to this problem would be very welcome.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Wed Mar 13, 2013 3:12 am 
Newbie

Joined: Wed Mar 13, 2013 3:01 am
Posts: 1
Anyone?


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Thu Mar 14, 2013 7:40 am 
Beginner
Beginner

Joined: Mon Nov 26, 2012 4:08 am
Posts: 24
lovelycoser wrote:
Anyone?


We have now done a downgrade as a workaround for this problem.
The last stable working version seems to be 4.1.2.Final, this version works for us.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Fri Mar 29, 2013 2:38 pm 
Newbie

Joined: Thu Mar 28, 2013 1:30 pm
Posts: 1
I was able to fix this error in my code - this was the problem.

Through some bad logic I had created this:

Type1 contains one Type2
-- Type2 contains a list of Type3s
----Type3 "ASDF"
----Type3 "qwert"
----Type3 "ASDF"
----Type3 "qwert"

Type2 had a list of Type3s. I had inadvertently broken the .equals() method on Type3 so when I added them to the list in Type2 duplicates were created. Hibernate used to apparently handle this fine (via map de-duplication?), but now it checks and if the map returns a null (aka the object is already in it) it throws an error.

So my message to you is, put a break point in hibernate's method and see which object you have duplicates of.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Tue Apr 02, 2013 6:55 am 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
The reason for this error is that you have in the object graph that you are trying to persist multiple objects that:

- Have the same persistence identifier (database id)
- Are not equal (which is either due to having different object identifiers if you haven't overridden equals/hashcode, or have different contents if you have overridden these methods)

Since Hibernate then won't know which of these objects that is the correct representation to be persisted, it throws an exception.


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Mon Nov 11, 2013 2:00 pm 
Newbie

Joined: Mon Nov 11, 2013 1:55 pm
Posts: 1
Well, from what I can find on my research, which includes the breakpoint. Hibernate now requires me to break the normal hashcode and equals contract of a bean to define it to mean only the 'business key' values. Last I looked Hibernate is supposed to be knowledgeable of database info, so why oh why does this not use the id of the entity? This is not rocket science. Yes, it can violate the contract of hash and set in theory. But, if you are inserting then it really can't violate equals can it? If it is there in a set, the bean is not equals. After persistence it is still not equals. Yes, the hash changes. This is at most a potential issue with performance. Really, Really think you got this very wrong! I should not have to change my contracts due to a framework. Fail!


Top
 Profile  
 
 Post subject: Re: On cascade error with entityManager
PostPosted: Tue Feb 11, 2014 6:00 am 
Newbie

Joined: Tue Feb 11, 2014 5:51 am
Posts: 1
This is 100% a Hibernate bug. The way I imagine it should work is that an Entity cannot contain multiple subentities that have the same database id but aren't equal. Instead, what currently happens is that hibernate ignores the database id, and equals/hashcode are used to determine whether an subentity is a copy. This is of course ridiculous, there are a myriad valid usecases in which I might give an entity two different copies of a subentity... being banal, if I have an Owner -> Cat -> Favorite Fish mapping, and the owner has two Cats who both like tuna, then the Owner can no longer be persisted @_@

I've found two "solutions" to my problem so far -

1. Downgrade to an earlier version of Hibernate;
2. Ruin the equals/hashcode method of the duplicated bean.

Both are garbage. I'll edit if I find something more reasonable.

Edit: I made a reasonable workaround - don't cascade the superelement automatically, do it "by hand" instead.

Returning to my Owner->Cat->Fish example: instead of relying on the CascadeType option to automatically cascade (ex: persist) the Cat and Fish, I now manually loop over the Cats of the Owner, persist each cat, and then persist the owner. This is annoying, but it works, and is not difficult to implement.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.