-->
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.  [ 1 post ] 
Author Message
 Post subject: An entity copy was already assigned to a different entity.
PostPosted: Wed Jun 05, 2013 2:15 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I just fixed an issue that bit me in the behind.

Basically, I got a "An entity copy was already assigned to a different entity." exception

A test case that recreates this scenario goes like so:

Code:
public class Event{
@ManyToOne(cascade=CascadeType.MERGE,fetch=FetchType.LAZY)
   @JoinColumn(name="FK_CountrySiteId")
   @IndexedEmbedded(includePaths={"id"})
   @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region=CacheRegion.SITE)
   @AnalyzerDiscriminator(impl = LanguageDiscriminator.class)
   public CountrySite getCountrySite() {
      return countrySite;
   }
}


Code:
      //detach countrysite
      mgr.clearSession();
      
      Usor user = mgr.getUser("marc@gmail.com");
      
      try{
         
         SearchProfile prof = user.getSearchProfile(countrySite);
         
         if(prof == null){
            prof = new SearchProfile((CountrySite) mgr.get(CountrySite.class,countrySite.getId()), user);
            user.getSearchProfiles().add(prof);
         }
         
         user = mgr.save(user);
         
         eventMgr.createAccountEvent(EventType.EDITPROFILE, countrySite, user, null);
         
         assertTrue(false);
         
      }catch(Exception e){
         
         assertTrue(true);
         
         countrySite = (CountrySite) mgr.get(CountrySite.class,countrySite.getId());
         
         SearchProfile prof = user.getSearchProfile(countrySite);
         
         if(prof == null){
            prof = new SearchProfile(countrySite, user);
            user.getSearchProfiles().add(prof);
         }
         
         user = mgr.save(user);
         
         eventMgr.createAccountEvent(EventType.EDITPROFILE, countrySite, user, null);
      }


So, that happens is that in eventMgr.createAccountEvent, I pass the detached countrySite that I just refreshed from the database in prof = new SearchProfile. This apparently makes Hibernate think there are two different objects that are equal.

That detached countrySite is set to another object Event (with CascadeType.MERGE) in the createAccountEvent method and saved, generating the exception.

I guess I just don't get why this happens. Could it be a problem with equals and hashcode? Or can you not use the detached object after you refreshed from the database. (Granted, this is a little inelegant, but should it lead to an exception?)

Code:
CountrySite:

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((country == null) ? 0 : country.hashCode());
      result = prime * result + ((site == null) ? 0 : site.hashCode());
      result = prime * result + ((url == null) ? 0 : url.hashCode());
      return result;
   }
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (!getClass().isAssignableFrom(obj.getClass()))
         return false;
      CountrySite other = (CountrySite) obj;
      if (url == null) {
         if (other.getUrl() != null)
            return false;
      } else if (!url.equals(other.getUrl()))
         return false;
      if (getSite() == null) {
         if (other.getSite() != null)
            return false;
      } else if (!getSite().equals(other.getSite()))
         return false;
      if (getCountry() == null) {
         if (other.getCountry() != null)
            return false;
      } else if (!getCountry().equals(other.getCountry()))
         return false;
      
      return true;
   }


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

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.