Search in sources :

Example 56 with HibernateProxy

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

the class DefaultEvictEventListener method onEvict.

/**
 * Handle the given evict event.
 *
 * @param event The evict event to be handled.
 *
 * @throws HibernateException
 */
public void onEvict(EvictEvent event) throws HibernateException {
    final Object object = event.getObject();
    if (object == null) {
        throw new NullPointerException("null passed to Session.evict()");
    }
    final EventSource source = event.getSession();
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    if (object instanceof HibernateProxy) {
        final LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        final Serializable id = li.getIdentifier();
        if (id == null) {
            throw new IllegalArgumentException("Could not determine identifier of proxy passed to evict()");
        }
        final EntityPersister persister = source.getFactory().getEntityPersister(li.getEntityName());
        final EntityKey key = source.generateEntityKey(id, persister);
        persistenceContext.removeProxy(key);
        if (!li.isUninitialized()) {
            final Object entity = persistenceContext.removeEntity(key);
            if (entity != null) {
                EntityEntry e = persistenceContext.removeEntry(entity);
                doEvict(entity, key, e.getPersister(), event.getSession());
            }
        }
        li.unsetSession();
    } else {
        EntityEntry e = persistenceContext.getEntry(object);
        if (e != null) {
            doEvict(object, e.getEntityKey(), e.getPersister(), source);
        } else {
            // see if the passed object is even an entity, and if not throw an exception
            // this is different than legacy Hibernate behavior, but what JPA 2.1 is calling for
            // with EntityManager.detach
            EntityPersister persister = null;
            final String entityName = persistenceContext.getSession().guessEntityName(object);
            if (entityName != null) {
                try {
                    persister = persistenceContext.getSession().getFactory().getEntityPersister(entityName);
                } catch (Exception ignore) {
                }
            }
            if (persister == null) {
                throw new IllegalArgumentException("Non-entity object instance passed to evict : " + object);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) HibernateException(org.hibernate.HibernateException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 57 with HibernateProxy

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

the class DefaultSaveOrUpdateEventListener method onSaveOrUpdate.

/**
 * Handle the given update event.
 *
 * @param event The update event to be handled.
 */
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();
    final Serializable requestedId = event.getRequestedId();
    if (requestedId != null) {
        // reassociating the proxy
        if (object instanceof HibernateProxy) {
            ((HibernateProxy) object).getHibernateLazyInitializer().setIdentifier(requestedId);
        }
    }
    // For an uninitialized proxy, noop, don't even need to return an id, since it is never a save()
    if (reassociateIfUninitializedProxy(object, source)) {
        LOG.trace("Reassociated uninitialized proxy");
    } else {
        // initialize properties of the event:
        final Object entity = source.getPersistenceContext().unproxyAndReassociate(object);
        event.setEntity(entity);
        event.setEntry(source.getPersistenceContext().getEntry(entity));
        // return the id in the event object
        event.setResultId(performSaveOrUpdate(event));
    }
}
Also used : Serializable(java.io.Serializable) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 58 with HibernateProxy

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

the class AnyType method guessEntityPersister.

private EntityPersister guessEntityPersister(Object object) {
    if (scope == null) {
        return null;
    }
    String entityName = null;
    // this code is largely copied from Session's bestGuessEntityName
    Object entity = object;
    if (entity instanceof HibernateProxy) {
        final LazyInitializer initializer = ((HibernateProxy) entity).getHibernateLazyInitializer();
        if (initializer.isUninitialized()) {
            entityName = initializer.getEntityName();
        }
        entity = initializer.getImplementation();
    }
    if (entityName == null) {
        for (EntityNameResolver resolver : scope.getTypeConfiguration().getSessionFactory().getMetamodel().getEntityNameResolvers()) {
            entityName = resolver.resolveEntityName(entity);
            if (entityName != null) {
                break;
            }
        }
    }
    if (entityName == null) {
        // the old-time stand-by...
        entityName = object.getClass().getName();
    }
    return scope.getTypeConfiguration().getSessionFactory().getMetamodel().entityPersister(entityName);
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityNameResolver(org.hibernate.EntityNameResolver) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 59 with HibernateProxy

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

the class ProxyTest method testProxySerialization.

@Test
public void testProxySerialization() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dp = new DataPoint();
    dp.setDescription("a data point");
    dp.setX(new BigDecimal(1.0));
    dp.setY(new BigDecimal(2.0));
    s.persist(dp);
    s.flush();
    s.clear();
    dp = (DataPoint) s.load(DataPoint.class, new Long(dp.getId()));
    assertFalse(Hibernate.isInitialized(dp));
    dp.getId();
    assertFalse(Hibernate.isInitialized(dp));
    dp.getDescription();
    assertTrue(Hibernate.isInitialized(dp));
    Object none = s.load(DataPoint.class, new Long(666));
    assertFalse(Hibernate.isInitialized(none));
    t.commit();
    s.disconnect();
    Object[] holder = new Object[] { s, dp, none };
    holder = (Object[]) SerializationHelper.clone(holder);
    Session sclone = (Session) holder[0];
    dp = (DataPoint) holder[1];
    none = holder[2];
    // close the original:
    s.close();
    t = sclone.beginTransaction();
    DataPoint sdp = (DataPoint) sclone.load(DataPoint.class, new Long(dp.getId()));
    assertSame(dp, sdp);
    assertFalse(sdp instanceof HibernateProxy);
    Object snone = sclone.load(DataPoint.class, new Long(666));
    assertSame(none, snone);
    assertTrue(snone instanceof HibernateProxy);
    sclone.delete(dp);
    t.commit();
    sclone.close();
}
Also used : Transaction(org.hibernate.Transaction) HibernateProxy(org.hibernate.proxy.HibernateProxy) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 60 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project ma-core-public by infiniteautomation.

the class H3PropertyDescriptorProperty method getValue.

/* (non-Javadoc)
     * @see org.directwebremoting.impl.PropertyDescriptorProperty#getValue(java.lang.Object)
     */
public Object getValue(Object bean) throws MarshallException {
    if (!(bean instanceof HibernateProxy)) {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    } else {
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized) {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }
        if (initialized) {
            return super.getValue(bean);
        } else {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen()) {
                return super.getValue(bean);
            }
            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H3SessionAjaxFilter.getCurrentSession(context);
            if (session != null) {
                session.update(bean);
                return super.getValue(bean);
            }
            return null;
        }
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) SessionImplementor(org.hibernate.engine.SessionImplementor) ServletContext(javax.servlet.ServletContext) 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