use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testDetachedSetReadOnlyAfterEvictViaSession.
@Test
public void testDetachedSetReadOnlyAfterEvictViaSession() {
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);
assertTrue(s.contains(dp));
s.evict(dp);
assertFalse(s.contains(dp));
assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
try {
s.setReadOnly(dp, true);
fail("should have failed because proxy was detached");
} catch (TransientObjectException ex) {
// expected
assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
} finally {
s.delete(dp);
s.getTransaction().commit();
s.close();
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testModifiableViaLazyInitializerBeforeInit.
@Test
public void testModifiableViaLazyInitializerBeforeInit() {
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(dp instanceof HibernateProxy);
assertTrue(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
checkReadOnly(s, dp, false);
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("changed", 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 testReadOnlyProxyMergeDetachedEntityWithChange.
@Test
public void testReadOnlyProxyMergeDetachedEntityWithChange() {
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));
s.getTransaction().commit();
s.close();
// modify detached proxy target
DataPoint dpEntity = (DataPoint) ((HibernateProxy) dp).getHibernateLazyInitializer().getImplementation();
dpEntity.setDescription("changed");
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dpLoaded = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dpLoaded instanceof HibernateProxy);
checkReadOnly(s, dpLoaded, false);
s.setReadOnly(dpLoaded, true);
checkReadOnly(s, dpLoaded, true);
assertFalse(Hibernate.isInitialized(dpLoaded));
DataPoint dpMerged = (DataPoint) s.merge(dpEntity);
assertSame(dpLoaded, dpMerged);
assertTrue(Hibernate.isInitialized(dpLoaded));
assertEquals("changed", dpLoaded.getDescription());
checkReadOnly(s, dpLoaded, 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 testModifiableViaSessionBeforeInitByModifiableQuery.
@Test
public void testModifiableViaSessionBeforeInitByModifiableQuery() {
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);
DataPoint dpFromQuery = (DataPoint) s.createQuery("from DataPoint where id=" + dpOrig.getId()).setReadOnly(false).uniqueResult();
assertTrue(Hibernate.isInitialized(dpFromQuery));
assertSame(dp, dpFromQuery);
checkReadOnly(s, dp, false);
dp.setDescription("changed");
assertEquals("changed", dp.getDescription());
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("changed", 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 testReadOnlyEntityMergeDetachedProxyWithChange.
@Test
public void testReadOnlyEntityMergeDetachedProxyWithChange() {
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));
s.getTransaction().commit();
s.close();
// modify detached proxy
dp.setDescription("changed");
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dpEntity = (DataPoint) s.get(DataPoint.class, new Long(dpOrig.getId()));
assertFalse(dpEntity instanceof HibernateProxy);
assertFalse(s.isReadOnly(dpEntity));
s.setReadOnly(dpEntity, true);
assertTrue(s.isReadOnly(dpEntity));
DataPoint dpMerged = (DataPoint) s.merge(dp);
assertSame(dpEntity, dpMerged);
assertEquals("changed", dpEntity.getDescription());
assertTrue(s.isReadOnly(dpEntity));
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();
}
Aggregations