Search in sources :

Example 86 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.

the class SerializableProxy method readResolve.

private Object readResolve() {
    HibernateProxy proxy = ByteBuddyProxyFactory.deserializeProxy(this);
    setReadOnlyBeforeAttachedToSession((ByteBuddyInterceptor) proxy.getHibernateLazyInitializer());
    return proxy;
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 87 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.

the class JavassistProxyFactory method deserializeProxy.

public static HibernateProxy deserializeProxy(SerializableProxy serializableProxy) {
    final JavassistLazyInitializer initializer = new JavassistLazyInitializer(serializableProxy.getEntityName(), serializableProxy.getPersistentClass(), serializableProxy.getInterfaces(), serializableProxy.getId(), resolveIdGetterMethod(serializableProxy), resolveIdSetterMethod(serializableProxy), serializableProxy.getComponentIdType(), null, ReflectHelper.overridesEquals(serializableProxy.getPersistentClass()));
    final javassist.util.proxy.ProxyFactory factory = buildJavassistProxyFactory(serializableProxy.getPersistentClass(), serializableProxy.getInterfaces());
    // note: interface is assumed to already contain HibernateProxy.class
    try {
        final Class proxyClass = factory.createClass();
        final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
        ((Proxy) proxy).setHandler(initializer);
        initializer.constructed();
        return proxy;
    } catch (Throwable t) {
        final String message = LOG.bytecodeEnhancementFailed(serializableProxy.getEntityName());
        LOG.error(message, t);
        throw new HibernateException(message, t);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) HibernateProxy(org.hibernate.proxy.HibernateProxy) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 88 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.

the class JavassistProxyFactory method getProxy.

@Override
public HibernateProxy getProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
    final JavassistLazyInitializer initializer = new JavassistLazyInitializer(entityName, persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals);
    try {
        final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
        ((Proxy) proxy).setHandler(initializer);
        initializer.constructed();
        return proxy;
    } catch (Throwable t) {
        LOG.error(LOG.bytecodeEnhancementFailed(entityName), t);
        throw new HibernateException(LOG.bytecodeEnhancementFailed(entityName), t);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) HibernateProxy(org.hibernate.proxy.HibernateProxy) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 89 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.

the class GetIdentifierTest method testProxyObject.

@Test
@TestForIssue(jiraKey = "HHH-7561")
public void testProxyObject() {
    EntityManager em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    try {
        Book book = new Book();
        em.persist(book);
        em.flush();
        // Clear persistence context to receive proxy object below.
        em.clear();
        Book proxy = em.getReference(Book.class, book.getId());
        assertTrue(proxy instanceof HibernateProxy);
        assertEquals(book.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(proxy));
    } finally {
        em.getTransaction().rollback();
        em.close();
    }
    em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    try {
        Author author = new Author();
        Article article = new Article(author);
        em.persist(author);
        em.persist(article);
        em.flush();
        // Clear persistence context to receive proxy relation below.
        em.clear();
        article = em.find(Article.class, article.getId());
        assertTrue(article.getAuthor() instanceof HibernateProxy);
        assertEquals(author.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(article.getAuthor()));
    } finally {
        em.getTransaction().rollback();
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) HibernateProxy(org.hibernate.proxy.HibernateProxy) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 90 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method multiPropProjectionNoTransformerDynNonLazy.

private void multiPropProjectionNoTransformerDynNonLazy(CacheMode sessionCacheMode, boolean isCacheableQuery) {
    Session s = openSession();
    s.setCacheMode(sessionCacheMode);
    Transaction t = s.beginTransaction();
    List resultList = s.createCriteria(Enrolment.class).setCacheable(isCacheableQuery).setFetchMode("student", FetchMode.JOIN).setProjection(Projections.projectionList().add(Property.forName("student"), "student").add(Property.forName("semester"), "semester").add(Property.forName("year"), "year").add(Property.forName("course"), "course")).addOrder(Order.asc("studentNumber")).list();
    t.commit();
    s.close();
    assertEquals(2, resultList.size());
    Object[] yogiObjects = (Object[]) resultList.get(0);
    Object[] shermanObjects = (Object[]) resultList.get(1);
    assertEquals(4, yogiObjects.length);
    assertTrue(yogiObjects[0] instanceof Student);
    assertTrue(Hibernate.isInitialized(yogiObjects[0]));
    assertEquals(yogiEnrolmentExpected.getSemester(), ((Short) yogiObjects[1]).shortValue());
    assertEquals(yogiEnrolmentExpected.getYear(), ((Short) yogiObjects[2]).shortValue());
    assertEquals(courseExpected, yogiObjects[3]);
    assertTrue(shermanObjects[0] instanceof Student);
    assertTrue(Hibernate.isInitialized(shermanObjects[0]));
    assertEquals(shermanEnrolmentExpected.getSemester(), ((Short) shermanObjects[1]).shortValue());
    assertEquals(shermanEnrolmentExpected.getYear(), ((Short) shermanObjects[2]).shortValue());
    assertTrue(!(shermanObjects[3] instanceof HibernateProxy));
    assertTrue(shermanObjects[3] instanceof Course);
    assertEquals(courseExpected, shermanObjects[3]);
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session)

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