Example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

List of usage examples for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

Introduction

In this page you can find the example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer.

Prototype

public LazyInitializer getHibernateLazyInitializer();

Source Link

Document

Get the underlying lazy initialization handler.

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override// ww  w .  j ava  2 s.  c  o  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:ch.algotrader.dao.HibernateInitializer.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T extends BaseEntityI> T initializeProxy(BaseEntityI entity, String context, T proxy) {

    if (proxy instanceof HibernateProxy) {

        HibernateProxy hibernateProxy = (HibernateProxy) proxy;
        LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();

        if (initializer.getSession() != null) {

            long before = System.nanoTime();
            proxy = (T) initializer.getImplementation();
            MetricsUtil.account(ClassUtils.getShortClassName(entity.getClass()) + context, (before));
        } else {/*from  w w w . jav  a  2  s  .  c  o m*/
            throw new IllegalStateException("no hibernate session available");
        }
    }

    return proxy;
}

From source file:ch.puzzle.itc.mobiliar.business.utils.ReflectionUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
static <T> T unproxyObject(T object) {
    if (object instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) object;
        return (T) hibernateProxy.getHibernateLazyInitializer().getImplementation();
    }// w  w w  .j  av a  2 s  .c  o  m
    return null;
}

From source file:com.apress.progwt.server.gwt.HibernateFilter.java

License:Apache License

public static Object filter(Object instance) {
    if (instance == null) {
        return instance;
    }/*from w ww .  j  a  v  a 2s.  com*/
    if (instance instanceof Date) {
        return new java.util.Date(((java.util.Date) instance).getTime());
    }

    if (instance instanceof PersistentSet) {
        HashSet<Object> hashSet = new HashSet<Object>();
        PersistentSet persSet = (PersistentSet) instance;
        if (persSet.wasInitialized()) {
            hashSet.addAll(persSet);
        }
        return hashSet;
    }
    if (instance instanceof PersistentList) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        PersistentList persList = (PersistentList) instance;
        if (persList.wasInitialized()) {
            arrayList.addAll(persList);
        }
        return arrayList;
    }
    if (instance instanceof PersistentBag) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        PersistentBag persBag = (PersistentBag) instance;
        if (persBag.wasInitialized()) {
            arrayList.addAll(persBag);
        }
        return arrayList;
    }
    if (instance instanceof PersistentMap) {
        HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
        PersistentMap persMap = (PersistentMap) instance;
        if (persMap.wasInitialized()) {
            hashMap.putAll(persMap);
        }
        return hashMap;
    }
    if (instance.getClass().getName().contains("CGLIB")) {

        if (Hibernate.isInitialized(instance)) {

            try {
                HibernateProxy hp = (HibernateProxy) instance;
                LazyInitializer li = hp.getHibernateLazyInitializer();
                log.warn("On The Fly initialization: " + li.getEntityName());
                return li.getImplementation();

            } catch (ClassCastException c) {
                log.error("error casting to HibernateProxy " + instance);
                return null;
            }

            // Hibernate.initialize(instance);
            //
            //               
            // log.warn("\nentity: " + cg.getEntityName()
            // + "\nidentifier" + cg.getIdentifier()
            // + "\nimplemenation " + cg.getImplementation());
            //
            // log.warn("On The Fly initialization: " + instance
            // + " now: " + instance.getClass().getName());
            //
            // if (instance instanceof ReallyCloneable) {
            // log.debug(instance.getClass().getName()
            // + " CGLIB Cloning " + instance);
            // return ((ReallyCloneable) instance).clone();
            // } else {
            // log
            // .warn("Initialized, but doesn't implement
            // ReallyCloneable"
            // + instance.getClass()
            // + " "
            // + instance.getClass().getName());
            // throw new CouldntFixCGLIBException(
            // instance.getClass()
            // + " must implement ReallyCloneable if we're to fix
            // it.");
            // }
        } else {
            log.debug("Uninitialized CGLIB");
            return null;
        }
    }

    return instance;
}

From source file:com.autobizlogic.abl.data.hibernate.HibPersistentBean.java

License:Open Source License

/**
 * If the object is a HibernateProxy, get the underlying object.
 */// w w  w. jav  a  2 s . c  o  m
private static Object getRawBean(Object o) {
    if (o instanceof HibernateProxy) {
        HibernateProxy hp = (HibernateProxy) o;
        o = hp.getHibernateLazyInitializer().getImplementation();
    }

    return o;
}

From source file:com.autobizlogic.abl.session.LogicTransactionContext.java

License:Open Source License

/**
 * Given an object, which could be either an entity or a proxy, get its primary key.
 *//*  ww  w  .  j a  v  a 2 s.c o  m*/
public Serializable getPrimaryKeyForObject(Object object) {
    if (object == null)
        return null;

    Serializable fk = null;
    if (object instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) object;
        fk = proxy.getHibernateLazyInitializer().getIdentifier();
    } else {
        SessionFactory sessionFactory = session.getSessionFactory();
        ClassMetadata classMeta = sessionFactory.getClassMetadata(object.getClass());
        fk = classMeta.getIdentifier(object, (SessionImplementor) session);
    }
    return fk;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public <T> T unproxy(T entity) {
    if (entity instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) entity;
        LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
        return (T) initializer.getImplementation();
    } else {//from   w  w w.j  a  v a  2  s  . c  o  m
        return entity;
    }
}

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

License:Apache License

@Override
public Object clone(CloningContext context, Object object) {
    ///*from   w ww  .jav  a2  s.c  o  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:com.fharms.marshalling.utils.HibernateDetachUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <If, Impl extends If> Impl clean(final If object) {
    if (isProxy(object)) {
        final HibernateProxy proxy = (HibernateProxy) object;
        final Impl impl = (Impl) proxy.getHibernateLazyInitializer().getImplementation();
        Hibernate.initialize(impl);//from   w w  w  .java2  s  .co  m
        return impl;
    } else if (isSerializableProxy(object)) {
        Hibernate.initialize(object);
        return (Impl) object;
    }
    return (Impl) object;
}

From source file:com.foo.server.rpc.hibernate.HibernateFilter.java

License:Apache License

public static Object filter(Object instance) {
    if (instance == null) {
        return instance;
    }//from w  ww.j  a  v a  2s. c  om
    if (instance instanceof Date) {
        return new java.util.Date(((java.util.Date) instance).getTime());
    }

    if (instance instanceof PersistentSet) {
        HashSet<Object> hashSet = new HashSet<Object>();
        PersistentSet persSet = (PersistentSet) instance;
        if (persSet.wasInitialized()) {
            hashSet.addAll(persSet);
        }
        return hashSet;
    }
    if (instance instanceof PersistentList) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        PersistentList persList = (PersistentList) instance;
        if (persList.wasInitialized()) {
            arrayList.addAll(persList);
        }
        return arrayList;
    }
    if (instance instanceof PersistentBag) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        PersistentBag persBag = (PersistentBag) instance;
        if (persBag.wasInitialized()) {
            arrayList.addAll(persBag);
        }
        return arrayList;
    }
    if (instance instanceof PersistentMap) {
        HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
        PersistentMap persMap = (PersistentMap) instance;
        if (persMap.wasInitialized()) {
            hashMap.putAll(persMap);
        }
        return hashMap;
    }
    //If use older Hibernate versions.
    //if( instance.getClass().getName().contains( "CGLIB" ) )
    if (instance.getClass().getName().contains("javassist")) {

        if (Hibernate.isInitialized(instance)) {

            try {
                HibernateProxy hp = (HibernateProxy) instance;
                LazyInitializer li = hp.getHibernateLazyInitializer();
                logger.warn("On The Fly initialization: " + li.getEntityName());
                return li.getImplementation();

            } catch (ClassCastException c) {
                logger.error("error casting to HibernateProxy " + instance);
                return null;
            }
        } else {
            logger.debug("Uninitialized javassist");
            return null;
        }
    }

    return instance;
}