package com.ecep.contract.util;

import org.hibernate.proxy.HibernateProxy;

import java.util.Objects;

public class HibernateProxyUtils {
    private static boolean useProxy = true;

    public static void useProxy(boolean b) {
        useProxy = b;
    }

    public static boolean isNotEffectiveClassEquals(Object the, Object that) {
        if (!useProxy) {
            return false;
        }
        Class<?> oEffectiveClass = the instanceof HibernateProxy
                ? ((HibernateProxy) the).getHibernateLazyInitializer().getPersistentClass()
                : the.getClass();
        Class<?> thisEffectiveClass = that instanceof HibernateProxy
                ? ((HibernateProxy) that).getHibernateLazyInitializer().getPersistentClass()
                : that.getClass();
        return thisEffectiveClass != oEffectiveClass;
    }

    public static int hashCode(Object that) {
        if (!useProxy) {
            return that.getClass().hashCode();
        }
        return that instanceof HibernateProxy
                ? ((HibernateProxy) that).getHibernateLazyInitializer().getPersistentClass().hashCode()
                : that.getClass().hashCode();
    }

}
