use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerAfterInit.
@Test
public void testReadOnlyViaLazyInitializerAfterInit() {
DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
LazyInitializer dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
assertTrue(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
checkReadOnly(s, dp, false);
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
s.flush();
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
assertEquals(dpOrig.getId(), dp.getId());
assertEquals(dpOrig.getDescription(), dp.getDescription());
assertEquals(dpOrig.getX(), dp.getX());
assertEquals(dpOrig.getY(), dp.getY());
s.delete(dp);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testSetReadOnlyAfterSessionClosed.
@Test
public void testSetReadOnlyAfterSessionClosed() {
DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
assertFalse(Hibernate.isInitialized(dp));
checkReadOnly(s, dp, false);
s.getTransaction().commit();
s.close();
try {
s.setReadOnly(dp, true);
fail("should have failed because session was closed");
} catch (IllegalStateException ex) {
// expected
assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
} finally {
s = openSession();
s.beginTransaction();
s.delete(dp);
s.getTransaction().commit();
s.close();
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method reassociateIfUninitializedProxy.
@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
if (!Hibernate.isInitialized(value)) {
final HibernateProxy proxy = (HibernateProxy) value;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
reassociateProxy(li, proxy);
return true;
} else {
return false;
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SessionImpl method bestGuessEntityName.
@Override
public String bestGuessEntityName(Object object) {
if (object instanceof HibernateProxy) {
LazyInitializer initializer = ((HibernateProxy) object).getHibernateLazyInitializer();
// so make certain that we do not accidentally initialize an uninitialized proxy
if (initializer.isUninitialized()) {
return initializer.getEntityName();
}
object = initializer.getImplementation();
}
EntityEntry entry = persistenceContext.getEntry(object);
if (entry == null) {
return guessEntityName(object);
} else {
return entry.getPersister().getEntityName();
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SessionImpl method getCurrentLockMode.
@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
checkOpen();
checkTransactionSynchStatus();
if (object == null) {
throw new NullPointerException("null object passed to getCurrentLockMode()");
}
if (object instanceof HibernateProxy) {
object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(this);
if (object == null) {
return LockMode.NONE;
}
}
EntityEntry e = persistenceContext.getEntry(object);
if (e == null) {
throw new TransientObjectException("Given object not associated with the session");
}
if (e.getStatus() != Status.MANAGED) {
throw new ObjectDeletedException("The given object was deleted", e.getId(), e.getPersister().getEntityName());
}
return e.getLockMode();
}
Aggregations