Example usage for org.hibernate.proxy LazyInitializer getEntityName

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

Introduction

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

Prototype

String getEntityName();

Source Link

Document

The entity-name of the entity our owning proxy represents.

Usage

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  w w  . j a va2s.  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.foo.server.rpc.hibernate.HibernateFilter.java

License:Apache License

public static Object filter(Object instance) {
    if (instance == null) {
        return instance;
    }/* 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 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 w  w.ja  v  a 2  s .  co  m
        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.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  . 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 (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: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 {//w w w.  j  a va2  s  .  c  o m
            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. j av  a  2s . c om*/
            getHibernateTemplate().initialize(proxy);
        }
    }
    return proxy;
}

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

License:Open Source License

/**
 *
 * @param proxy/*from   ww  w  .  ja  v a2  s .com*/
 * @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.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  .ja  v a2 s  . c  om*/
        }
        // 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   ww  w .ja  va  2 s  . c  o m*/
        }
    }

    return cls;
}