use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class MultiPathCascadeTest method testMultiPathMergeModifiedDetachedIntoProxy.
@Test
public void testMultiPathMergeModifiedDetachedIntoProxy() throws Exception {
// persist a simple A in the database
Session s = openSession();
s.beginTransaction();
A a = new A();
a.setData("Anna");
s.save(a);
s.getTransaction().commit();
s.close();
// modify detached entity
modifyEntity(a);
s = openSession();
s.beginTransaction();
A aLoaded = (A) s.load(A.class, new Long(a.getId()));
assertTrue(aLoaded instanceof HibernateProxy);
assertSame(aLoaded, s.merge(a));
s.getTransaction().commit();
s.close();
verifyModifications(a.getId());
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method reassociateProxy.
@Override
public void reassociateProxy(Object value, Serializable id) throws MappingException {
if (value instanceof HibernateProxy) {
LOG.debugf("Setting proxy identifier: %s", id);
final HibernateProxy proxy = (HibernateProxy) value;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
li.setIdentifier(id);
reassociateProxy(li, proxy);
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method isReadOnly.
@Override
public boolean isReadOnly(Object entityOrProxy) {
if (entityOrProxy == null) {
throw new AssertionFailure("object must be non-null.");
}
boolean isReadOnly;
if (entityOrProxy instanceof HibernateProxy) {
isReadOnly = ((HibernateProxy) entityOrProxy).getHibernateLazyInitializer().isReadOnly();
} else {
final EntityEntry ee = getEntry(entityOrProxy);
if (ee == null) {
throw new TransientObjectException("Instance was not associated with this persistence context");
}
isReadOnly = ee.isReadOnly();
}
return isReadOnly;
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method setReadOnly.
@Override
public void setReadOnly(Object object, boolean readOnly) {
if (object == null) {
throw new AssertionFailure("object must be non-null.");
}
if (isReadOnly(object) == readOnly) {
return;
}
if (object instanceof HibernateProxy) {
final HibernateProxy proxy = (HibernateProxy) object;
setProxyReadOnly(proxy, readOnly);
if (Hibernate.isInitialized(proxy)) {
setEntityReadOnly(proxy.getHibernateLazyInitializer().getImplementation(), readOnly);
}
} else {
setEntityReadOnly(object, readOnly);
// PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
// so need to check the return value to be sure it is really a proxy
final Object maybeProxy = getSession().getPersistenceContext().proxyFor(object);
if (maybeProxy instanceof HibernateProxy) {
setProxyReadOnly((HibernateProxy) maybeProxy, readOnly);
}
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method narrowProxy.
@Override
@SuppressWarnings("unchecked")
public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object) throws HibernateException {
final Class concreteProxyClass = persister.getConcreteProxyClass();
final boolean alreadyNarrow = concreteProxyClass.isInstance(proxy);
if (!alreadyNarrow) {
LOG.narrowingProxy(concreteProxyClass);
// It would just be extra processing. Just return the impl
if (object != null) {
proxiesByKey.remove(key);
return object;
}
// Similarly, if the original HibernateProxy is initialized, there
// is again no point in creating a proxy. Just return the impl
final HibernateProxy originalHibernateProxy = (HibernateProxy) proxy;
if (!originalHibernateProxy.getHibernateLazyInitializer().isUninitialized()) {
final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation();
// can we return it?
if (concreteProxyClass.isInstance(impl)) {
proxiesByKey.remove(key);
return impl;
}
}
// Otherwise, create the narrowed proxy
final HibernateProxy narrowedProxy = (HibernateProxy) persister.createProxy(key.getIdentifier(), session);
// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly();
narrowedProxy.getHibernateLazyInitializer().setReadOnly(readOnlyOrig);
return narrowedProxy;
} else {
if (object != null) {
final LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
li.setImplementation(object);
}
return proxy;
}
}
Aggregations