HibernateUtils.java

package com.tradecloud.common.base;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;

/**
 *
 */
public class HibernateUtils {
    private static final Logger log = Logger.getLogger(HibernateUtils.class);

    /**
     * This is for use in equals methods.
     *
     * @param obj1 The object that might be a proxy.
     * @param obj2 The other object that might be a proxy.
     * @return True if the two objects have the same underlying class.
     */
    public static boolean proxyClassEquals(Object obj1, Object obj2) {
        return obj1 != null && obj2 != null ? getAClass(obj1).equals(getAClass(obj2)) : false;
    }

    private static Class<?> getAClass(Object obj1) {
        return getNonProxyObject(obj1).getClass();
    }

    /**
     * Use this instead of instanceof.
     *
     * @param obj1 The object that might be a proxy.
     * @param c1   The non-proxy class you want to check.
     * @return True if the object has the underlying class
     */
    public static boolean proxyClassIs(Object obj1, Class<?> c1) {
        return getNonProxyObject(obj1).getClass().equals(c1);
    }

    public static boolean proxyClassInstanceOf(Object obj, Class<?> c) {
        return c.isAssignableFrom(getAClass(obj));
    }

    /**
     * Retrieve the actual object from Hibernate if a proxied version is supplied.  This is achieved
     * using Hibernate helper classes and methods.
     * <p>
     * If the supplied object is not a proxy then the same object is returned (as it is already the non-proxied version).
     *
     * @param potentialProxy The entity (potentially a proxy) whose real object we want to return
     * @return The actual object of the supplied {@code potentialProxy} object
     */
    public static <T> T getNonProxyObject(T potentialProxy) {
        if (potentialProxy instanceof HibernateProxy) {
            HibernateProxy hibernateProxy = (HibernateProxy) potentialProxy;
            LazyInitializer hibernateLazyInitializer = hibernateProxy.getHibernateLazyInitializer();
            if (!hibernateLazyInitializer.isUninitialized()) {
                T implementation = (T) hibernateLazyInitializer.getImplementation();
                return implementation;
            }
        }
        return potentialProxy;
    }

    public static boolean isUninitialized(Object potentialProxy) {
        if (potentialProxy instanceof HibernateProxy) {
            HibernateProxy hibernateProxy = (HibernateProxy) potentialProxy;
            LazyInitializer hibernateLazyInitializer = hibernateProxy.getHibernateLazyInitializer();
            if (!hibernateLazyInitializer.isUninitialized()) {
                return true;
            }
        }
        return false;
    }

    public static <T> T initializeAndUnproxy(T entity) {
        if (entity == null) {
//            throw new
//                    NullPointerException("Entity passed for initialization is null");
            return null;
        }

        Hibernate.initialize(entity);
        if (entity instanceof HibernateProxy) {
            entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
                    .getImplementation();
        }
        return entity;
    }
}