use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class LazyGroupUpdateTestTask method execute.
@Override
public void execute() {
Session s = getFactory().openSession();
s.beginTransaction();
Child c1 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "steve").uniqueResult();
// verify the expected initial loaded state
assertLoaded(c1, "name");
assertNotLoaded(c1, "nickName");
assertNotLoaded(c1, "parent");
assertNotLoaded(c1, "alternateParent");
// Now lets update nickName which ought to initialize nickName and parent, but not alternateParent
c1.setNickName("new nickName");
assertLoaded(c1, "nickName");
assertNotLoaded(c1, "parent");
assertNotLoaded(c1, "alternateParent");
assertEquals("Hibernate", c1.getParent().getNombre());
assertFalse(c1.getParent() instanceof HibernateProxy);
// Now update c1.parent
c1.getParent().getChildren().remove(c1);
Parent p1New = new Parent();
p1New.setNombre("p1New");
c1.setParent(p1New);
p1New.getChildren().add(c1);
s.getTransaction().commit();
s.close();
s = getFactory().openSession();
s.getTransaction().begin();
c1 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "steve").uniqueResult();
// verify updates
assertEquals("new nickName", c1.getNickName());
assertEquals("p1New", c1.getParent().getNombre());
assertFalse(c1.getParent() instanceof HibernateProxy);
s.getTransaction().commit();
s.close();
s = getFactory().openSession();
s.getTransaction().begin();
Child c2 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "sally").uniqueResult();
// verify the expected initial loaded state
assertLoaded(c2, "name");
assertNotLoaded(c2, "nickName");
assertNotLoaded(c2, "parent");
assertNotLoaded(c2, "alternateParent");
// Now lets access and update alternateParent which ought to initialize alternateParent and nothing else
Parent p1 = c2.getAlternateParent();
c2.setAlternateParent(p1New);
assertNotLoaded(c2, "nickName");
assertNotLoaded(c2, "parent");
assertLoaded(c2, "alternateParent");
assertEquals("p1New", c2.getAlternateParent().getNombre());
assertFalse(c2.getAlternateParent() instanceof HibernateProxy);
p1.getAlternateChildren().remove(c2);
p1New.getAlternateChildren().add(c2);
s.getTransaction().commit();
s.close();
s = getFactory().openSession();
s.getTransaction().begin();
c2 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "sally").uniqueResult();
// verify update
assertEquals("p1New", c2.getAlternateParent().getNombre());
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class LazyLoadingTestTask method execute.
public void execute() {
Session s = getFactory().openSession();
s.beginTransaction();
Child loadedChild = s.load(Child.class, lastChildID);
Object nameByReflection = EnhancerTestUtils.getFieldByReflection(loadedChild, "name");
Assert.assertNotNull("Non-lazy field 'name' was not loaded", nameByReflection);
Object parentByReflection = EnhancerTestUtils.getFieldByReflection(loadedChild, "parent");
Assert.assertNull("Lazy field 'parent' is initialized", parentByReflection);
Assert.assertFalse(loadedChild instanceof HibernateProxy);
Parent loadedParent = loadedChild.getParent();
assertThat(loadedChild.getName(), notNullValue());
assertThat(loadedParent, notNullValue());
assertThat(loadedChild.getParent(), notNullValue());
EnhancerTestUtils.checkDirtyTracking(loadedChild);
parentByReflection = EnhancerTestUtils.getFieldByReflection(loadedChild, "parent");
Object childrenByReflection = EnhancerTestUtils.getFieldByReflection(loadedParent, "children");
Assert.assertNotNull("Lazy field 'parent' is not loaded", parentByReflection);
Assert.assertNull("Lazy field 'children' is initialized", childrenByReflection);
Assert.assertFalse(loadedParent instanceof HibernateProxy);
Assert.assertTrue(parentID.equals(loadedParent.id));
Collection<Child> loadedChildren = loadedParent.getChildren();
EnhancerTestUtils.checkDirtyTracking(loadedChild);
EnhancerTestUtils.checkDirtyTracking(loadedParent);
childrenByReflection = EnhancerTestUtils.getFieldByReflection(loadedParent, "children");
Assert.assertNotNull("Lazy field 'children' is not loaded", childrenByReflection);
Assert.assertFalse(loadedChildren instanceof HibernateProxy);
Assert.assertEquals(CHILDREN_SIZE, loadedChildren.size());
Assert.assertTrue(loadedChildren.contains(loadedChild));
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class DiscriminatorTest method testLoadSuperclassProxyEvictPolymorphicAccess.
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
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, new Long(e.getId()));
Employee pQuery = (Employee) s.createQuery("from Person where id = :id").setLong("id", e.getId()).uniqueResult();
Employee pCriteria = (Employee) s.createCriteria(Person.class).add(Restrictions.idEq(new Long(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 DiscriminatorTest method testLoadSuperclassProxyPolymorphicAccess.
@Test
public void testLoadSuperclassProxyPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
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, new Long(e.getId()));
Person pQuery = (Person) s.createQuery("from Person where id = :id").setLong("id", e.getId()).uniqueResult();
Person pCriteria = (Person) s.createCriteria(Person.class).add(Restrictions.idEq(new Long(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 EmbeddedCompositeIdTest method testPolymorphism.
@Test
public void testPolymorphism() {
Session s = openSession();
Transaction t = s.beginTransaction();
Course uc = new UniversityCourse("mat2000", "Monash", "second year maths", 0);
Course c = new Course("eng5000", "BHS", "grade 5 english");
s.persist(uc);
s.persist(c);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Course ucid = new Course("mat2000", "Monash", null);
Course cid = new Course("eng5000", "BHS", null);
Course luc = (Course) s.load(Course.class, ucid);
Course lc = (Course) s.load(Course.class, cid);
assertFalse(Hibernate.isInitialized(luc));
assertFalse(Hibernate.isInitialized(lc));
assertEquals(UniversityCourse.class, Hibernate.getClass(luc));
assertEquals(Course.class, Hibernate.getClass(lc));
assertSame(((HibernateProxy) lc).getHibernateLazyInitializer().getImplementation(), cid);
assertEquals(c.getCourseCode(), "eng5000");
assertEquals(uc.getCourseCode(), "mat2000");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
ucid = new Course("mat2000", "Monash", null);
cid = new Course("eng5000", "BHS", null);
luc = (Course) s.get(Course.class, ucid);
lc = (Course) s.get(Course.class, cid);
assertTrue(Hibernate.isInitialized(luc));
assertTrue(Hibernate.isInitialized(lc));
assertEquals(UniversityCourse.class, Hibernate.getClass(luc));
assertEquals(Course.class, Hibernate.getClass(lc));
assertSame(lc, cid);
assertEquals(c.getCourseCode(), "eng5000");
assertEquals(uc.getCourseCode(), "mat2000");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List list = s.createQuery("from Course order by courseCode").list();
assertTrue(list.get(0) instanceof Course);
assertTrue(list.get(1) instanceof UniversityCourse);
c = (Course) list.get(0);
uc = (UniversityCourse) list.get(1);
assertEquals(c.getCourseCode(), "eng5000");
assertEquals(uc.getCourseCode(), "mat2000");
t.commit();
s.close();
c.setDescription("Grade 5 English");
uc.setDescription("Second year mathematics");
s = openSession();
t = s.beginTransaction();
s.saveOrUpdate(c);
s.saveOrUpdate(uc);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(c);
s.delete(uc);
t.commit();
s.close();
}
Aggregations