Example usage for org.hibernate.proxy LazyInitializer getIdentifier

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

Introduction

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

Prototype

Serializable getIdentifier();

Source Link

Document

Retrieve the identifier value for the entity our owning proxy represents.

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 w  w. ja va  2  s  . 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 {/*from   w w w.ja v a  2  s .  c  o  m*/
        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;
    }//from   w w  w .j  a v  a  2s.  co  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// w  ww  .  ja v a2 s.c om
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) {
    ///*w  w  w  .  j  a  va  2  s .  c om*/
    // 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:org.beangle.commons.orm.hibernate.HibernateEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> T initialize(T proxy) {
    if (proxy instanceof HibernateProxy) {
        LazyInitializer init = ((HibernateProxy) proxy).getHibernateLazyInitializer();
        if (null == init.getSession() || init.getSession().isClosed()) {
            proxy = (T) getSession().get(init.getEntityName(), init.getIdentifier());
        } else {/*from  w  w  w  . j  ava 2  s . c om*/
            Hibernate.initialize(proxy);
        }
    } else if (proxy instanceof PersistentCollection) {
        Hibernate.initialize(proxy);
    }
    return proxy;
}

From source file:org.beangle.model.persist.hibernate.HibernateEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> T initialize(T proxy) {
    if (proxy instanceof HibernateProxy) {
        LazyInitializer init = ((HibernateProxy) proxy).getHibernateLazyInitializer();
        if (null == init.getSession() || init.getSession().isClosed()) {
            proxy = (T) getHibernateTemplate().get(init.getEntityName(), init.getIdentifier());
        } else {//www . ja  v a 2  s .  c  o  m
            getHibernateTemplate().initialize(proxy);
        }
    }
    return proxy;
}

From source file:org.gaia.dao.audit.AuditAccessor.java

License:Open Source License

/**
 *
 * @param proxy//from   w ww. j  a  v  a2 s.co  m
 * @return
 * @throws ClassNotFoundException
 */
public static Object loadLazyObject(HibernateProxy proxy) throws ClassNotFoundException {
    LazyInitializer init = proxy.getHibernateLazyInitializer();
    logger.debug(init.getEntityName() + StringUtils.SPACE + init.getIdentifier());
    return HibernateUtil.getObject(Class.forName(init.getEntityName()), init.getIdentifier());
}

From source file:org.nextframework.persistence.HibernateUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <E> E getLazyValue(E value) {
    if (value instanceof HibernateProxy) {
        LazyInitializer hibernateLazyInitializer = ((HibernateProxy) value).getHibernateLazyInitializer();
        if (hibernateLazyInitializer.isUninitialized()) {
            try {
                Class<?> superclass = value.getClass().getSuperclass();
                Serializable identifier = hibernateLazyInitializer.getIdentifier();

                value = loadValue(value, superclass, identifier);
            } catch (IllegalArgumentException e) {
            } catch (SecurityException e) {
            }/*from ww  w.ja v a 2s.  c  o m*/
        }
    } else if (value instanceof PersistentCollection) {
        try {
            PersistentCollection collection = (PersistentCollection) value;
            if (!collection.wasInitialized()) {
                Object owner = collection.getOwner();
                String role = collection.getRole();
                value = (E) DAOUtils.getDAOForClass(owner.getClass()).loadCollection(owner,
                        role.substring(role.lastIndexOf('.') + 1));
                System.out.println("COLECAO LAZY " + owner.getClass().getSimpleName() + "." + role);
            }
        } catch (Exception e) {
            //se nao conseguir carregar o valor lazy, no fazer nada
        }
    }
    return value;
}

From source file:org.openvpms.component.business.dao.hibernate.im.common.DOState.java

License:Open Source License

/**
 * Returns a key for an object, to be used in sets and maps, to avoid
 * loading objects from the database./*from ww  w . j a v a2 s . c  om*/
 * <p/>
 * If the object is loaded, its link identifier will be used, otherwise
 * the concatenation of its persistent class name and id will be used.
 *
 * @param object the object
 * @return a unique identifier for the object
 */
private String getKey(IMObjectDO object) {
    String id = null;
    if (object instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) object;
        LazyInitializer init = proxy.getHibernateLazyInitializer();
        if (init.isUninitialized()) {
            id = init.getPersistentClass().getName() + "#" + init.getIdentifier().toString();
        }
    }
    if (id == null) {
        id = object.getLinkId();
    }
    return id;
}