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;
}