hibernate JPA 使用懒加载时代理对象

hibernate延迟加载代理对象实际对象读取方式java

 

    public static <T>  T deproxy (T obj) {  
        if (obj == null)  
            return obj;  
        if (obj instanceof HibernateProxy) {  
            // Unwrap Proxy;  
            //      -- loading, if necessary.  
            HibernateProxy proxy = (HibernateProxy) obj;  
            LazyInitializer li = proxy.getHibernateLazyInitializer();  
            return (T)  li.getImplementation();  
        }   
        return obj;  
    }  

 

 

全部解决的问题数据库

当两个对象相互关联并使用懒加载时,从数据库中取出来使用时报错,经过调试查看对象全部字段的值为null;其中有个handle的对象,表明着为hibernater的缓存代理对象。但经过get\setXXX()有能获得该对象的字段值。可是 在不被get\setXXX()时获取原对象的类型时报错,提示 javax.persistence.EntityNotFoundException: Unable to find xxxxx缓存

解决描述工具

在使用hibernate从数据库加载含有子类的实体类对象时,检查其真实类型是很是必要的。由于可能取出来的是一个代理对象。因此须要在取完数据以后判断该对象是不是其自己,是或不是就返回自己。spa

提供的一个工具类,以下:hibernate

public class HbUtils {  
  
    public static <T>  T deproxy (T obj) {  
        if (obj == null)  
            return obj;  
        if (obj instanceof HibernateProxy) {  
            // Unwrap Proxy;  
            //      -- loading, if necessary.  
            HibernateProxy proxy = (HibernateProxy) obj;  
            LazyInitializer li = proxy.getHibernateLazyInitializer();  
            return (T)  li.getImplementation();  
        }   
        return obj;  
    }  
  
  
    public static boolean isProxy (Object obj) {  
        if (obj instanceof HibernateProxy)  
            return true;  
        return false;  
    }  
  
    // ----------------------------------------------------------------------------------  
  
  
    public static boolean isEqual (Object o1, Object o2) {  
        if (o1 == o2)  
            return true;  
        if (o1 == null || o2 == null)   
            return false;  
        Object d1 = deproxy(o1);  
        Object d2 = deproxy(o2);  
        if (d1 == d2 || d1.equals(d2))  
            return true;  
        return false;  
    }  
  
    public static boolean notEqual (Object o1, Object o2) {  
        return ! isEqual( o1, o2);  
    }  
  
    // ----------------------------------------------------------------------------------  
  
    public static boolean isSame (Object o1, Object o2) {  
        if (o1 == o2)  
            return true;  
        if (o1 == null || o2 == null)   
            return false;  
        Object d1 = deproxy(o1);  
        Object d2 = deproxy(o2);  
        if (d1 == d2)  
            return true;  
        return false;  
    }  
  
    public static boolean notSame (Object o1, Object o2) {  
        return ! isSame( o1, o2);  
    }  
  
  
  
    // ----------------------------------------------------------------------------------  
  
    public static Class getClassWithoutInitializingProxy (Object obj) {  
        if (obj instanceof HibernateProxy) {  
            HibernateProxy proxy = (HibernateProxy) obj;  
            LazyInitializer li = proxy.getHibernateLazyInitializer();  
            return li.getPersistentClass();  
        }   
        // Not a Proxy.  
        return obj.getClass();  
    }  
  
} 
相关文章
相关标签/搜索