use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ImmutableTest method testChangeImmutableEntityProxyToModifiable.
@Test
public void testChangeImmutableEntityProxyToModifiable() {
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(c, 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 ParentChildTest method testLazyManyToOneCriteria.
@Test
public void testLazyManyToOneCriteria() {
Session s = openSession();
Transaction t = s.beginTransaction();
Baz baz = new Baz();
s.save(baz);
Foo foo1 = new Foo();
s.save(foo1);
baz.setFoo(foo1);
s.flush();
s.clear();
baz = (Baz) s.createCriteria(Baz.class).uniqueResult();
assertTrue(Hibernate.isInitialized(baz.getFoo()));
assertFalse(baz.getFoo() instanceof HibernateProxy);
t.rollback();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ParentChildTest method testLazyManyToOneGet.
@Test
public void testLazyManyToOneGet() {
Session s = openSession();
Transaction t = s.beginTransaction();
Baz baz = new Baz();
s.save(baz);
Foo foo1 = new Foo();
s.save(foo1);
baz.setFoo(foo1);
s.flush();
s.clear();
baz = (Baz) s.get(Baz.class, baz.getCode());
assertTrue(Hibernate.isInitialized(baz.getFoo()));
assertFalse(baz.getFoo() instanceof HibernateProxy);
t.rollback();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ParentChildTest method testLazyManyToOneHQL.
@Test
public void testLazyManyToOneHQL() {
Session s = openSession();
Transaction t = s.beginTransaction();
Baz baz = new Baz();
s.save(baz);
Foo foo1 = new Foo();
s.save(foo1);
baz.setFoo(foo1);
s.flush();
s.clear();
baz = (Baz) s.createQuery("from Baz b").uniqueResult();
assertFalse(Hibernate.isInitialized(baz.getFoo()));
assertTrue(baz.getFoo() instanceof HibernateProxy);
t.rollback();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ProxyNarrowingTest method testNarrowedProxyIsInitializedIfOriginalProxyIsInitialized.
@Test
public void testNarrowedProxyIsInitializedIfOriginalProxyIsInitialized() {
Session session = openSession();
Integer entityReferenceId = null;
// Populate the database
try {
Transaction t = session.beginTransaction();
ConcreteEntity entity = new ConcreteEntity();
session.save(entity);
LazyAbstractEntityReference reference = new LazyAbstractEntityReference(entity);
session.save(reference);
entityReferenceId = reference.getId();
session.flush();
t.commit();
} finally {
session.close();
}
session = openSession();
try {
session.beginTransaction();
// load a proxified version of the entity into the session: the proxy is based on the AbstractEntity class
// as the reference class property is of type AbstractEntity.
LazyAbstractEntityReference reference = session.get(LazyAbstractEntityReference.class, entityReferenceId);
AbstractEntity abstractEntityProxy = reference.getEntity();
assertTrue((abstractEntityProxy instanceof HibernateProxy) && !Hibernate.isInitialized(abstractEntityProxy));
Hibernate.initialize(abstractEntityProxy);
assertTrue(Hibernate.isInitialized(abstractEntityProxy));
// load the concrete class via session.load to trigger the StatefulPersistenceContext.narrowProxy code
ConcreteEntity concreteEntityProxy = session.load(ConcreteEntity.class, abstractEntityProxy.getId());
// the new proxy created should be initialized
assertTrue(Hibernate.isInitialized(concreteEntityProxy));
assertTrue(session.contains(concreteEntityProxy));
// clean up
session.delete(reference);
session.delete(concreteEntityProxy);
session.getTransaction().commit();
} finally {
session.close();
}
}
Aggregations