Search in sources :

Example 11 with HibernateProxy

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

Example 12 with HibernateProxy

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

Example 13 with HibernateProxy

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

Example 14 with HibernateProxy

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

Example 15 with HibernateProxy

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