Example usage for org.hibernate.proxy LazyInitializer getPersistentClass

List of usage examples for org.hibernate.proxy LazyInitializer getPersistentClass

Introduction

In this page you can find the example usage for org.hibernate.proxy LazyInitializer getPersistentClass.

Prototype

Class getPersistentClass();

Source Link

Document

Get the actual class of the entity.

Usage

From source file:cc.alcina.framework.entity.impl.jboss.EntityCacheHibernateResolvingFilter.java

License:Apache License

@Override
public <T> T filterData(T value, T cloned, GraphProjectionContext context, GraphProjection graphCloner)
        throws Exception {
    if (value instanceof HasIdAndLocalId) {
        HasIdAndLocalId hili = (HasIdAndLocalId) value;
        if (ensureInjected != null && ensureInjected.containsKey(hili)) {
            hili = ensureInjected.get(hili);
            ensureInjected.remove(hili);
            // if it does, just proceed as normal (hili will already be the
            // key in the reached map, so .project() wouldn't work)
            if (hili != value) {
                hili = graphCloner.project(hili, value, context, false);
                getCache().put((HasIdAndLocalId) hili);
                return (T) hili;
            }//  w ww  .  ja  v  a  2s.c om
        }
        if (value instanceof HibernateProxy) {
            LazyInitializer lazy = ((HibernateProxy) value).getHibernateLazyInitializer();
            Serializable id = lazy.getIdentifier();
            Class persistentClass = lazy.getPersistentClass();
            Object impl = getCache().get(persistentClass, (Long) id);
            if (impl == null) {
                if (useRawDomainStore) {
                    if (DomainStore.stores().writableStore().isCachedTransactional(persistentClass)) {
                        impl = (T) Domain.find(persistentClass, (Long) id);
                    }
                }
            }
            if (impl == null && instantiateImplCallback != null) {
                if (instantiateImplCallback.instantiateLazyInitializer(lazy, context)) {
                    impl = ((HibernateProxy) value).getHibernateLazyInitializer().getImplementation();
                    impl = graphCloner.project(impl, value, context, false);
                    getCache().put((HasIdAndLocalId) impl);
                } else if (shellInstantiator != null) {
                    impl = shellInstantiator.instantiateShellObject(lazy, context);
                    if (impl != null) {
                        getCache().put((HasIdAndLocalId) impl);
                    }
                }
            }
            if (impl != null) {
                return (T) impl;
            } else {
                // Serializable identifier = ((HibernateProxy) value)
                // .getHibernateLazyInitializer().getIdentifier();
                // System.out
                // .format("discarded %s: %s\n", context, identifier);
                return null;
            }
        } else {
            Class<? extends HasIdAndLocalId> valueClass = hili.getClass();
            Object cached = getCache().get(valueClass, hili.getId());
            if (cached != null) {
                return (T) cached;
            } else {
                if (useRawDomainStore) {
                    if (DomainStore.stores().writableStore().isCached(valueClass)) {
                        return (T) Domain.find(valueClass, hili.getId());
                    }
                }
                HasIdAndLocalId clonedHili = (HasIdAndLocalId) cloned;
                clonedHili.setId(hili.getId());
                getCache().put(clonedHili);
                return (T) clonedHili;
            }
        }
    }
    return super.filterData(value, cloned, context, graphCloner);
}

From source file:cc.alcina.framework.entity.impl.jboss.JPAHibernateImpl.java

License:Apache License

@Override
public String entityDebugString(Object object) {
    try {/*  ww  w.ja va2s.c om*/
        if (object instanceof HibernateProxy) {
            LazyInitializer lazy = ((HibernateProxy) object).getHibernateLazyInitializer();
            Serializable id = lazy.getIdentifier();
            Class clazz = lazy.getPersistentClass();
            return String.format("\tclass: %s\n\tid:\t%s\n\n", clazz, id);
        }
        if (object instanceof HasIdAndLocalId) {
            return object.toString();
        }
    } catch (Exception e) {
        // stale transaction e.g.
        if (object instanceof HasIdAndLocalId) {
            HiliHelper.asDomainPoint((HasId) object);
        }
    }
    return null;
}

From source file:cc.alcina.framework.entity.impl.jboss.JPAHibernateImpl.java

License:Apache License

private HiliLocator toHiliLocator(Object o) {
    if (o == null) {
        return null;
    }/* w  ww.  j  a v  a2s. c o m*/
    if (o instanceof HibernateProxy) {
        LazyInitializer lazy = ((HibernateProxy) o).getHibernateLazyInitializer();
        return new HiliLocator(lazy.getPersistentClass(), (Long) lazy.getIdentifier(), 0L);
    }
    return new HiliLocator((HasIdAndLocalId) o);
}

From source file:ch.algotrader.cache.EntityHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w ww .  j a  va  2 s .co m
protected CacheResponse initialize(Object obj) {

    if (!(obj instanceof HibernateProxy)) {
        throw new IllegalArgumentException("none HibernateProxy passed " + obj);
    }

    synchronized (obj) {

        HibernateProxy proxy = (HibernateProxy) obj;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        Object initializedObj = this.cacheManager.getGenericDao().get(initializer.getPersistentClass(),
                (Long) initializer.getIdentifier());

        CacheResponse response = this.cacheManager.put(initializedObj);

        if (response.getState() == CacheState.EXISTING) {
            return response;
        } else {
            return CacheResponse.updatedObject(initializedObj);
        }
    }
}

From source file:com.erinors.hpb.server.handler.UninitializedHibernateProxyHandler.java

License:Apache License

@Override
public Object clone(CloningContext context, Object object) {
    ///*from   www.  j  ava  2 s . co m*/
    // Check if the object is an uninitialized hibernate proxy, return false otherwise
    //

    if (!(object instanceof HibernateProxy)) {
        return null;
    }

    HibernateProxy hibernateProxy = (HibernateProxy) object;
    LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();

    if (!lazyInitializer.isUninitialized()) {
        return null;
    }

    //
    // Check if the proxied object can be cloned
    //

    Class<?> persistentClass = lazyInitializer.getPersistentClass();
    if (!HibernateProxyPojoSupport.class.isAssignableFrom(persistentClass)) {
        // TODO tesztet r
        throw new RuntimeException("Uninitialized hibernate proxy should implement "
                + HibernateProxyPojoSupport.class.getName() + " to be cloneable: " + persistentClass);
    }

    //
    // Clone
    //

    HibernateProxyPojoSupport result;
    try {
        Constructor<?> constructor = ClassUtils.getAccessibleInstanceConstructor(persistentClass);
        result = (HibernateProxyPojoSupport) constructor.newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Cannot instantiate: " + persistentClass.getClass(), e);
    }

    result.setUninitializedHibernateProxy(true);
    result.setUninitializedHibernateProxyId(lazyInitializer.getIdentifier());

    context.addProcessedObject(object, result);

    return result;
}

From source file:net.sf.beanlib.hibernate.UnEnhancer.java

License:Apache License

public static <T> Class<T> getActualClass(Object object) {
    Class<?> c = object.getClass();
    boolean enhanced = true;

    while (c != null && enhanced) {
        enhanced = isCheckCGLib() && Enhancer.isEnhanced(c) || isJavassistEnhanced(c);
        if (enhanced) {
            if (object instanceof HibernateProxy) {
                HibernateProxy hibernateProxy = (HibernateProxy) object;
                LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
                try {
                    // suggested by Chris (harris3@sourceforge.net)
                    Object impl = lazyInitializer.getImplementation();

                    if (impl != null) {
                        @SuppressWarnings("unchecked")
                        Class<T> ret = (Class<T>) impl.getClass();
                        return ret;
                    }/*from   w ww.j  av a2  s  . co  m*/
                } catch (HibernateException ex) {
                    Logger.getLogger(UnEnhancer.class)
                            .warn("Unable to retrieve the underlying persistent object", ex);
                }
                @SuppressWarnings("unchecked")
                Class<T> ret = lazyInitializer.getPersistentClass();
                return ret;
            }
            c = c.getSuperclass();
        }
    }
    @SuppressWarnings("unchecked")
    Class<T> ret = (Class<T>) c;
    return ret;
}

From source file:org.granite.hibernate.HibernateClassGetter.java

License:Open Source License

@Override
public Class<?> getClass(Object o) {

    if (o instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) o;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();

        String className = (initializer.isUninitialized() ? initializer.getEntityName()
                : initializer.getImplementation().getClass().getName());

        if (className != null && className.length() > 0) {
            try {
                return TypeUtil.forName(className);
            } catch (Exception e) {
                log.warn(e, "Could not get class with initializer: %s for: %s",
                        initializer.getClass().getName(), className);
            }/* w w  w.j a va 2 s  .c o  m*/
        }
        // fallback...
        return initializer.getPersistentClass();
    }

    return super.getClass(o);
}

From source file:org.granite.hibernate.HibernateExternalizer.java

License:Open Source License

protected String getProxyDetachedState(HibernateProxy proxy) {
    LazyInitializer initializer = proxy.getHibernateLazyInitializer();

    StringBuilder sb = new StringBuilder();

    sb.append(initializer.getClass().getName()).append(':');
    if (initializer.getPersistentClass() != null)
        sb.append(initializer.getPersistentClass().getName());
    sb.append(':');
    if (initializer.getEntityName() != null)
        sb.append(initializer.getEntityName());

    return sb.toString();
}

From source file:org.granite.hibernate.jmf.EntityCodec.java

License:Open Source License

protected Class<?> getClass(ExtendedObjectOutput out, Object v) {
    Class<?> cls = v.getClass();

    if (v instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) v).getHibernateLazyInitializer();

        String className = (initializer.isUninitialized() ? initializer.getEntityName()
                : initializer.getImplementation().getClass().getName());

        if (className != null && className.length() > 0) {
            try {
                cls = out.getReflection().loadClass(className);
            } catch (ClassNotFoundException e) {
                cls = initializer.getPersistentClass();
            }//from   w w w.j  a va 2 s  . c om
        }
    }

    return cls;
}

From source file:org.mifos.framework.components.audit.util.helpers.InterceptHelper.java

License:Open Source License

private Class getClazz(Object obj) {
    try {/*from  w w w . jav a2  s.c  o  m*/
        HibernateProxy hibernateProxy = (HibernateProxy) obj;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
        return lazyInitializer.getPersistentClass();
    } catch (ClassCastException e) {
        return obj.getClass();
    }
}