Search in sources :

Example 91 with HibernateProxy

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

Example 92 with HibernateProxy

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

Example 93 with HibernateProxy

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

Example 94 with HibernateProxy

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

Example 95 with HibernateProxy

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

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