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:org.brushingbits.jnap.common.bean.visitor.HibernateTypeResolver.java

License:Apache License

@Override
public Class<?> resolve(Object source, BeanPropertyFilter filter) {
    Class<?> type = null;/*from  w  ww.  ja  va 2s  .c  o m*/
    if (source instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) source;
        LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
        if (!lazyInitializer.isUninitialized() || !filter.isIgnoreUninitializedProxies()) {
            type = lazyInitializer.getImplementation().getClass();
        }
    } else if (source instanceof PersistentCollection) {
        PersistentCollection collection = (PersistentCollection) source;
        if (collection.wasInitialized() || !filter.isIgnoreUninitializedProxies()) {
            type = getUnderlyingCollectionType(collection.getClass());
        }
    } else {
        type = super.resolve(source, filter);
    }
    return type;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.HibernateProxyHandler.java

License:Apache License

public Object unwrapProxy(final HibernateProxy proxy) {
    final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
    if (lazyInitializer.isUninitialized()) {
        lazyInitializer.initialize();/*  w ww  . j av a  2  s .  c om*/
    }
    final Object obj = lazyInitializer.getImplementation();
    if (obj != null) {
        GrailsHibernateUtil.ensureCorrectGroovyMetaClass(obj, obj.getClass());
    }
    return obj;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.proxy.SimpleHibernateProxyHandler.java

License:Apache License

public Object unwrapProxy(final HibernateProxy proxy) {
    final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
    if (lazyInitializer.isUninitialized()) {
        lazyInitializer.initialize();// w  w w.  java  2s .  c om
    }
    final Object obj = lazyInitializer.getImplementation();
    if (obj != null) {
        ensureCorrectGroovyMetaClass(obj, obj.getClass());
    }
    return obj;
}

From source file:org.directwebremoting.hibernate.H3BeanConverter.java

License:Apache License

/**
 * Hibernate makes {@link Class#getClass()} difficult ...
 * @param example The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object//from  ww  w . ja va  2  s .com
 */
public Class<?> getClass(Object example) {
    if (example instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        SessionImplementor implementor = initializer.getSession();

        if (initializer.isUninitialized()) {
            try {
                // getImplementation is going to want to talk to a session
                if (implementor.isClosed()) {
                    // Give up and return example.getClass();
                    return example.getClass();
                }
            } catch (NoSuchMethodError ex) {
                // We must be using Hibernate 3.0/3.1 which doesn't have
                // this method
            }
        }

        return initializer.getImplementation().getClass();
    } else {
        return example.getClass();
    }
}

From source file:org.directwebremoting.hibernate.H3PropertyDescriptorProperty.java

License:Apache License

@Override
public Object getValue(Object bean) throws ConversionException {
    if (!(bean instanceof HibernateProxy)) {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    } else {/*from  ww w . j  a  v  a 2s  .  c om*/
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized) {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }

        if (initialized) {
            return super.getValue(bean);
        } else {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen()) {
                return super.getValue(bean);
            }

            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H3SessionAjaxFilter.getCurrentSession(context);

            if (session != null) {
                session.update(bean);
                return super.getValue(bean);
            }

            return null;
        }
    }
}

From source file:org.directwebremoting.hibernate.H4PropertyDescriptorProperty.java

License:Apache License

@Override
public Object getValue(Object bean) throws ConversionException {
    if (!(bean instanceof HibernateProxy)) {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    } else {//from  w  w w . j  av  a2  s .co m
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized) {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }

        if (initialized) {
            return super.getValue(bean);
        } else {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen()) {
                return super.getValue(bean);
            }

            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H4SessionAjaxFilter.getCurrentSession(context);

            if (session != null) {
                session.update(bean);
                return super.getValue(bean);
            }

            return null;
        }
    }
}

From source file:org.egov.infra.web.support.json.adapter.HibernateProxyTypeAdapter.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from  w  w  w.j  av a2 s  .  c  om
public void write(JsonWriter out, HibernateProxy value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    delegate.write(out, value.getHibernateLazyInitializer().getImplementation());
}

From source file:org.emonocot.model.marshall.json.hibernate.HibernateProxySerializer.java

License:Open Source License

/**
 * Helper method for finding value being proxied, if it is available
 * or if it is to be forced to be loaded.
 *//*from ww  w .  j  ava  2s.co  m*/
protected Object findProxied(HibernateProxy proxy) {
    LazyInitializer init = proxy.getHibernateLazyInitializer();
    if (!_forceLazyLoading && init.isUninitialized()) {
        return null;
    }
    return init.getImplementation();
}

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

License:Open Source License

/**
 *
 * @param proxy// ww  w .  j a va  2s  . c om
 * @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.geoserver.hibernate.AbstractHibFacade.java

License:Open Source License

protected Object first(final Query query, boolean doWarn) {
    query.setMaxResults(doWarn ? 2 : 1);
    query.setCacheable(true);//w ww .  j a va2 s.  c  o  m

    List<?> result = query.list();
    if (result.isEmpty()) {
        return null;
    } else {
        //TODO: add a flag to control exception
        if (result.size() > 1) {
            throw new RuntimeException("Expected 1 result from " + query + " but got " + result.size());

        }
        //            if (doWarn && result.size() > 1) {
        //                LOGGER.log(Level.WARNING, "Found too many items in result", new RuntimeException(
        //                        "Trace: Found too many items in query"));
        //            }

        Object ret = result.get(0);
        if (ret instanceof HibernateProxy) {
            HibernateProxy proxy = (HibernateProxy) ret;
            ret = proxy.getHibernateLazyInitializer().getImplementation();
        }

        if (LOGGER.isLoggable(Level.FINE)) {
            StringBuilder callerChain = new StringBuilder();
            for (StackTraceElement stackTraceElement : new Throwable().getStackTrace()) {
                if ("first".equals(stackTraceElement.getMethodName()))
                    continue;
                String cname = stackTraceElement.getClassName();
                if (cname.startsWith("org.spring"))
                    continue;
                cname = cname.substring(cname.lastIndexOf(".") + 1);
                callerChain.append(cname).append('.').append(stackTraceElement.getMethodName()).append(':')
                        .append(stackTraceElement.getLineNumber()).append(' ');
                // if(++num==10) break;
            }
            LOGGER.fine(
                    "FIRST -->" + ret.getClass().getSimpleName() + " --- " + ret + " { " + callerChain + "}");
        }
        return ret;
    }
}