use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testSetReadOnlyAfterSessionClosedViaLazyInitializer.
@Test
public void testSetReadOnlyAfterSessionClosedViaLazyInitializer() {
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();
assertTrue(s.contains(dp));
s.close();
assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
try {
((HibernateProxy) dp).getHibernateLazyInitializer().setReadOnly(true);
fail("should have failed because session was detached");
} catch (TransientObjectException 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 ReadOnlyProxyTest method testReadOnlyViaLazyInitializerBeforeInit.
@Test
public void testReadOnlyViaLazyInitializerBeforeInit() {
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);
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
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 testIsReadOnlyAfterSessionClosed.
@Test
public void testIsReadOnlyAfterSessionClosed() {
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.isReadOnly(dp);
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 ReadOnlyProxyTest method testReadOnlyViaSessionNoChangeAfterInit.
@Test
public void testReadOnlyViaSessionNoChangeAfterInit() {
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);
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
Hibernate.initialize(dp);
assertTrue(Hibernate.isInitialized(dp));
checkReadOnly(s, dp, false);
s.getTransaction().commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
s.setReadOnly(dp, true);
checkReadOnly(s, dp, true);
assertFalse(Hibernate.isInitialized(dp));
Hibernate.initialize(dp);
assertTrue(Hibernate.isInitialized(dp));
checkReadOnly(s, dp, true);
s.getTransaction().commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
s.setReadOnly(dp, true);
checkReadOnly(s, dp, true);
assertFalse(Hibernate.isInitialized(dp));
s.setReadOnly(dp, false);
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
Hibernate.initialize(dp);
assertTrue(Hibernate.isInitialized(dp));
checkReadOnly(s, dp, false);
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 EntityType method getHashCode.
@Override
public int getHashCode(Object x, SessionFactoryImplementor factory) {
EntityPersister persister = getAssociatedEntityPersister(factory);
if (!persister.canExtractIdOutOfEntity()) {
return super.getHashCode(x);
}
final Serializable id;
if (x instanceof HibernateProxy) {
id = ((HibernateProxy) x).getHibernateLazyInitializer().getIdentifier();
} else {
final Class mappedClass = persister.getMappedClass();
if (mappedClass.isAssignableFrom(x.getClass())) {
id = persister.getIdentifier(x);
} else {
id = (Serializable) x;
}
}
return persister.getIdentifierType().getHashCode(id, factory);
}
Aggregations