HHH-4735 : Proxy can be associated with a new session when it is already connected to a different one
git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_3@18321 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
5c62213ddd
commit
32173a3d6e
|
@ -46,6 +46,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
|
|||
private boolean unwrap;
|
||||
|
||||
private transient SessionImplementor session;
|
||||
private transient EntityKey entityKey; // cached value
|
||||
|
||||
/**
|
||||
* For serialization from the non-pojo initializers (HHH-3309)
|
||||
|
@ -64,6 +65,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
|
|||
this.entityName = entityName;
|
||||
this.id = id;
|
||||
this.session = session;
|
||||
this.entityKey = generateEntityKeyOrNull();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,6 +87,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
|
|||
*/
|
||||
public final void setIdentifier(Serializable id) {
|
||||
this.id = id;
|
||||
entityKey = generateEntityKeyOrNull();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,17 +113,31 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
|
|||
//TODO: perhaps this should be some other RuntimeException...
|
||||
throw new HibernateException("illegally attempted to associate a proxy with two open Sessions");
|
||||
}
|
||||
else if ( s == null ){
|
||||
unsetSession();
|
||||
}
|
||||
else {
|
||||
session = s;
|
||||
entityKey = generateEntityKeyOrNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private EntityKey generateEntityKeyOrNull() {
|
||||
if ( getIdentifier() == null || session == null || entityName == null ) {
|
||||
return null;
|
||||
}
|
||||
return new EntityKey(
|
||||
getIdentifier(), session.getFactory().getEntityPersister( entityName ), session.getEntityMode()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void unsetSession() {
|
||||
session = null;
|
||||
entityKey = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,9 +179,14 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
|
|||
* @return Value for property 'connectedToSession'.
|
||||
*/
|
||||
protected final boolean isConnectedToSession() {
|
||||
return session!=null &&
|
||||
session.isOpen() &&
|
||||
session.getPersistenceContext().containsProxy(this);
|
||||
return getProxyOrNull() != null;
|
||||
}
|
||||
|
||||
private Object getProxyOrNull() {
|
||||
if ( session != null && session.isOpen() && entityKey != null ) {
|
||||
return session.getPersistenceContext().getProxy( entityKey );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ import junit.framework.Test;
|
|||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
|
||||
import org.hibernate.intercept.FieldInterceptionHelper;
|
||||
|
@ -388,6 +389,23 @@ public class SaveOrUpdateTest extends FunctionalTestCase {
|
|||
assertTrue( s1.contains( child.getParent() ) );
|
||||
|
||||
Session s2 = openSession();
|
||||
try {
|
||||
s2.getTransaction().begin();
|
||||
s2.saveOrUpdate( child );
|
||||
fail();
|
||||
}
|
||||
catch ( HibernateException ex ) {
|
||||
// expected because parent is connected to s1
|
||||
}
|
||||
finally {
|
||||
s2.getTransaction().rollback();
|
||||
}
|
||||
s2.close();
|
||||
|
||||
s1.evict( child.getParent() );
|
||||
assertFalse( s1.contains( child.getParent() ) );
|
||||
|
||||
s2 = openSession();
|
||||
s2.getTransaction().begin();
|
||||
s2.saveOrUpdate( child );
|
||||
assertTrue( s2.contains( child ) );
|
||||
|
|
Loading…
Reference in New Issue