use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SimpleInheritanceTest method testLoadSuperclassProxyPolymorphicAccess.
@Test
public void testLoadSuperclassProxyPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
e.setId(7);
e.setName("Steve");
e.setSex('M');
e.setTitle("grand poobah");
s.save(e);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// load the superclass proxy.
Person pLoad = (Person) s.load(Person.class, new Long(e.getId()));
assertTrue(pLoad instanceof HibernateProxy);
Person pGet = (Person) s.get(Person.class, e.getId());
Person pQuery = (Person) s.createQuery("from org.hibernate.test.discriminator.Person where id = :id").setLong("id", e.getId()).uniqueResult();
Person pCriteria = (Person) s.createCriteria(Person.class).add(Restrictions.idEq(e.getId())).uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame(pLoad, pGet);
assertSame(pLoad, pQuery);
assertSame(pLoad, pCriteria);
// assert that the proxy is not an instance of Employee
assertFalse(pLoad instanceof Employee);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete(e);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SimpleInheritanceTest method testLoadSuperclassProxyEvictPolymorphicAccess.
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
e.setId(8);
e.setName("Steve");
e.setSex('M');
e.setTitle("grand poobah");
s.save(e);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// load the superclass proxy.
Person pLoad = (Person) s.load(Person.class, new Long(e.getId()));
assertTrue(pLoad instanceof HibernateProxy);
// evict the proxy
s.evict(pLoad);
Employee pGet = (Employee) s.get(Person.class, e.getId());
Employee pQuery = (Employee) s.createQuery("from org.hibernate.test.discriminator.Person where id = :id").setLong("id", e.getId()).uniqueResult();
Employee pCriteria = (Employee) s.createCriteria(Person.class).add(Restrictions.idEq(e.getId())).uniqueResult();
// assert that executing the queries polymorphically returns the same Employee instance
assertSame(pGet, pQuery);
assertSame(pGet, pCriteria);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete(e);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ImmutableTest method testChangeImmutableEntityToModifiable.
@Test
public void testChangeImmutableEntityToModifiable() {
Contract c = new Contract(null, "gavin", "phone");
ContractVariation cv1 = new ContractVariation(1, c);
cv1.setText("expensive");
ContractVariation cv2 = new ContractVariation(2, c);
cv2.setText("more expensive");
clearCounts();
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(c);
assertTrue(s.isReadOnly(c));
assertTrue(s.isReadOnly(cv1));
assertTrue(s.isReadOnly(cv2));
t.commit();
s.close();
assertInsertCount(3);
assertUpdateCount(0);
clearCounts();
s = openSession();
t = s.beginTransaction();
c = (Contract) s.createCriteria(Contract.class).uniqueResult();
assertTrue(s.isReadOnly(c));
assertEquals(c.getCustomerName(), "gavin");
assertEquals(c.getVariations().size(), 2);
Iterator it = c.getVariations().iterator();
cv1 = (ContractVariation) it.next();
assertEquals(cv1.getText(), "expensive");
cv2 = (ContractVariation) it.next();
assertEquals(cv2.getText(), "more expensive");
assertTrue(s.isReadOnly(cv1));
assertTrue(s.isReadOnly(cv2));
try {
assertTrue(c instanceof HibernateProxy);
s.setReadOnly(((HibernateProxy) c).getHibernateLazyInitializer().getImplementation(), false);
} catch (IllegalStateException ex) {
// expected
} finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
s.delete(c);
assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
t.commit();
s.close();
assertUpdateCount(0);
assertDeleteCount(3);
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlySessionLazyNonLazyTest method checkObject.
private void checkObject(Object entityOrProxy, Set expectedInitializedObjects, Set expectedReadOnlyObjects, Session s) {
boolean isExpectedToBeInitialized = expectedInitializedObjects.contains(entityOrProxy);
boolean isExpectedToBeReadOnly = expectedReadOnlyObjects.contains(entityOrProxy);
SessionImplementor si = (SessionImplementor) s;
assertEquals(isExpectedToBeInitialized, Hibernate.isInitialized(entityOrProxy));
assertEquals(isExpectedToBeReadOnly, s.isReadOnly(entityOrProxy));
if (Hibernate.isInitialized(entityOrProxy)) {
Object entity = (entityOrProxy instanceof HibernateProxy ? ((HibernateProxy) entityOrProxy).getHibernateLazyInitializer().getImplementation(si) : entityOrProxy);
assertNotNull(entity);
assertEquals(isExpectedToBeReadOnly, s.isReadOnly(entity));
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ReadOnlyCriteriaQueryTest method checkProxyReadOnly.
private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
assertTrue(proxy instanceof HibernateProxy);
LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
assertSame(s, li.getSession());
assertEquals(expectedReadOnly, s.isReadOnly(proxy));
assertEquals(expectedReadOnly, li.isReadOnly());
assertEquals(Hibernate.isInitialized(proxy), !li.isUninitialized());
if (Hibernate.isInitialized(proxy)) {
assertEquals(expectedReadOnly, s.isReadOnly(li.getImplementation()));
}
}
Aggregations