Example usage for org.hibernate.proxy LazyInitializer getImplementation

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

Introduction

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

Prototype

Object getImplementation();

Source Link

Document

Return the underlying persistent object, initializing if necessary

Usage

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  .  ja  v a2 s  . co  m
            throw new IllegalStateException("no hibernate session available");
        }
    }

    return proxy;
}

From source file:ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils.java

License:Apache License

/**
 * @return Unproxied <var>proxy</var>.
 *//*from   w  ww .  j  av a 2 s .  c o m*/
@SuppressWarnings({ "unchecked" })
public final static <T> T unproxy(final T proxy) {
    if (proxy instanceof HibernateProxy && Hibernate.isInitialized(proxy)) {
        LazyInitializer lazyInitializer = ((HibernateProxy) proxy).getHibernateLazyInitializer();
        SessionImplementor sessionImplementor = lazyInitializer.getSession();
        // check if the given bean still has a session instance attached
        if (sessionImplementor != null) {
            // use the unproxy method of the persistenceContext class
            return (T) sessionImplementor.getPersistenceContext().unproxy(proxy);
        } else {
            // return the wrapped bean instance if there's no active session instance available
            return (T) lazyInitializer.getImplementation();
        }
    } else {
        // not a proxy - nothing to do
        return proxy;
    }
}

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

License:Apache License

public static Object filter(Object instance) {
    if (instance == null) {
        return instance;
    }/*w w  w .j a  va  2 s  .  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.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 ww w.j av  a  2  s .  c o  m*/
        return entity;
    }
}

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

License:Apache License

@Override
public Object clone(final CloningContext context, Object object) {
    final Object effectiveObject;
    if (object instanceof HibernateProxy) {
        LazyInitializer lazyInitializer = ((HibernateProxy) object).getHibernateLazyInitializer();

        if (lazyInitializer.isUninitialized()) {
            throw new RuntimeException("Uninitialized proxies are not supported by this handler: " + object);
        }/*  w  w w  . j a v  a 2  s.co m*/

        effectiveObject = lazyInitializer.getImplementation();

        Object alreadyProcessedObject = context.getProcessedObject(effectiveObject);
        if (alreadyProcessedObject != null) {
            return alreadyProcessedObject;
        }
    } else {
        effectiveObject = object;
    }

    return copyBean(effectiveObject, new ObjectCopier() {
        @Override
        public Object processObject(Object object) {
            return context.clone(object);
        }
    }, context);
}

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 a2 s.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;
}

From source file:com.formation.adhesion.ods.web.server.service.hibernatefilter.HibernateFilter.java

License:Apache License

@SuppressWarnings("unchecked")
public static Object filter(Object input) {
    try {/*from   w ww .  j  a va 2 s  .  c  om*/
        RPC.getDefaultSerializationPolicy().validateSerialize(input.getClass());
        return input;
    } catch (SerializationException e1) {

        if (input == null) {
            return input;
        }
        if (input instanceof Date) {
            return new java.util.Date(((java.util.Date) input).getTime());
        }

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

            if (Hibernate.isInitialized(input)) {

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

                } catch (ClassCastException c) {
                    log.error("error casting to HibernateProxy " + input);
                    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;
            }
        }

        Class instanceClass = input.getClass();
        Method[] methods = instanceClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            try {
                if (methods[i].getName().startsWith("get")) {
                    Object value = methods[i].invoke(input);

                    // on doit donc filtrer cette valeur
                    Method setter = null;
                    try {
                        setter = instanceClass.getMethod("s" + methods[i].getName().substring(1),
                                methods[i].getReturnType());

                    } catch (Exception e) {
                        // la methode set correspondante n'existe pas
                    }
                    if (setter != null)
                        setter.invoke(input, filter(value));

                }
            } catch (InvocationTargetException e) {
                log.info("Call method " + methods[i].getName() + " failed on class " + instanceClass.getName());
            } catch (Exception e) {
                log.error(e);
            }
        }
        return input;
    }

}

From source file:com.github.dozermapper.core.util.HibernateProxyResolver.java

License:Apache License

@Override
public <T> T unenhanceObject(T object) {
    if (object instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) object;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();

        return (T) lazyInitializer.getImplementation();
    }/*from w ww  .  j  ava 2 s . com*/
    return object;
}

From source file:com.google.gwt.sample.dynatable.utils.HibernateFilter.java

License:Apache License

@SuppressWarnings("unchecked")
public static Object filter(Object instance) {
    if (instance == null) {
        return instance;
    }/*from   w  w  w . ja  va 2  s .c o m*/
    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.projectlaver.service.PaymentService.java

License:Open Source License

private <T> T deproxy(T obj) {
    if (obj == null) {
        return obj;
    }/*from w w w  . ja  va2s .c  o  m*/
    if (obj instanceof HibernateProxy) {
        // Unwrap Proxy;
        //      -- loading, if necessary.
        HibernateProxy proxy = (HibernateProxy) obj;
        LazyInitializer li = proxy.getHibernateLazyInitializer();
        return (T) li.getImplementation();
    }
    return obj;
}