Search in sources :

Example 6 with HibernateProxy

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();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session)

Example 7 with HibernateProxy

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();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session)

Example 8 with HibernateProxy

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();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with HibernateProxy

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();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with HibernateProxy

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)130 Session (org.hibernate.Session)58 Test (org.junit.Test)56 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 DefaultPostLoaderDao (org.broadleafcommerce.common.persistence.DefaultPostLoaderDao)16 PostLoaderDao (org.broadleafcommerce.common.persistence.PostLoaderDao)16 EntityEntry (org.hibernate.engine.spi.EntityEntry)13 Serializable (java.io.Serializable)11 Transaction (org.hibernate.Transaction)10 TransientObjectException (org.hibernate.TransientObjectException)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 HibernateException (org.hibernate.HibernateException)8 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Iterator (java.util.Iterator)3 ObjectDeletedException (org.hibernate.ObjectDeletedException)3 EventSource (org.hibernate.event.spi.EventSource)3 BigDecimal (java.math.BigDecimal)2