use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testReadOnlyViaSessionBeforeInitByReadOnlyQuery.
@Test
public void testReadOnlyViaSessionBeforeInitByReadOnlyQuery() {
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.setReadOnly(dp, true);
checkReadOnly(s, dp, true);
DataPoint dpFromQuery = (DataPoint) s.createQuery("from DataPoint where id=" + dpOrig.getId()).setReadOnly(true).uniqueResult();
assertTrue(Hibernate.isInitialized(dpFromQuery));
assertSame(dp, dpFromQuery);
checkReadOnly(s, dp, true);
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(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 testReadOnlyViaSessionDoesNotInit.
@Test
public void testReadOnlyViaSessionDoesNotInit() {
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));
s.setReadOnly(dp, true);
checkReadOnly(s, dp, true);
assertFalse(Hibernate.isInitialized(dp));
s.setReadOnly(dp, false);
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.flush();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.getTransaction().commit();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
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 testReadOnlyViaLazyInitializerDoesNotInit.
@Test
public void testReadOnlyViaLazyInitializerDoesNotInit() {
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();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
assertFalse(Hibernate.isInitialized(dp));
dpLI.setReadOnly(false);
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.flush();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
s.getTransaction().commit();
checkReadOnly(s, dp, false);
assertFalse(Hibernate.isInitialized(dp));
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 testModifiableViaSessionBeforeInitByReadOnlyQuery.
@Test
public void testModifiableViaSessionBeforeInitByReadOnlyQuery() {
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));
DataPoint dpFromQuery = (DataPoint) s.createQuery("from DataPoint where id=" + dpOrig.getId()).setReadOnly(true).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 testReadOnlyRefreshDeleted.
@Test
public void testReadOnlyRefreshDeleted() {
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
Transaction t = s.beginTransaction();
DataPoint dp = new DataPoint();
dp.setDescription("original");
dp.setX(new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN));
dp.setY(new BigDecimal(Math.cos(dp.getX().doubleValue())).setScale(19, BigDecimal.ROUND_DOWN));
s.save(dp);
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
HibernateProxy dpProxy = (HibernateProxy) s.load(DataPoint.class, dp.getId());
assertFalse(Hibernate.isInitialized(dpProxy));
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
dp = (DataPoint) s.get(DataPoint.class, dp.getId());
s.delete(dp);
s.flush();
try {
s.refresh(dp);
fail("should have thrown UnresolvableObjectException");
} catch (UnresolvableObjectException ex) {
// expected
} finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
s.setCacheMode(CacheMode.IGNORE);
DataPoint dpProxyInit = (DataPoint) s.load(DataPoint.class, dp.getId());
assertEquals("original", dp.getDescription());
s.delete(dpProxyInit);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
assertTrue(dpProxyInit instanceof HibernateProxy);
assertTrue(Hibernate.isInitialized(dpProxyInit));
try {
s.refresh(dpProxyInit);
fail("should have thrown UnresolvableObjectException");
} catch (UnresolvableObjectException ex) {
// expected
} finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
assertTrue(dpProxy instanceof HibernateProxy);
try {
s.refresh(dpProxy);
assertFalse(Hibernate.isInitialized(dpProxy));
Hibernate.initialize(dpProxy);
fail("should have thrown UnresolvableObjectException");
} catch (UnresolvableObjectException ex) {
// expected
} finally {
t.rollback();
s.close();
}
}
Aggregations