第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將Hibernate代理轉(zhuǎn)換為實(shí)際實(shí)體對(duì)象

如何將Hibernate代理轉(zhuǎn)換為實(shí)際實(shí)體對(duì)象

哆啦的時(shí)光機(jī) 2019-07-19 15:20:03
如何將Hibernate代理轉(zhuǎn)換為實(shí)際實(shí)體對(duì)象在冬眠期間Session,我正在加載一些對(duì)象,其中一些由于延遲加載而作為代理加載。沒關(guān)系,我不想把懶惰的負(fù)載關(guān)掉。但是稍后我需要通過RPC向GWT客戶機(jī)發(fā)送一些對(duì)象(實(shí)際上是一個(gè)對(duì)象)。碰巧這個(gè)具體的對(duì)象是一個(gè)代理。所以我需要把它變成一個(gè)真實(shí)的物體。我在Hibernate中找不到像“物化”這樣的方法。如何將一些對(duì)象從代理轉(zhuǎn)換為知道它們的類和ID的reals?目前,我看到的唯一解決方案是將該對(duì)象從Hibernate的緩存中刪除并重新加載,但是由于許多原因,它確實(shí)很糟糕。
查看完整描述

3 回答

?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是我用的一種方法。

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

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


查看完整回答
反對(duì) 回復(fù) 2019-07-19
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

因?yàn)镠ibernate ORM 5.2.10,您可以這樣做:

Object?unproxiedEntity?=?Hibernate.unproxy(?proxy?);

在冬眠之前5.2.10。最簡(jiǎn)單的方法是使用非代理Hibernate內(nèi)部提供的方法PersistenceContext執(zhí)行情況:

Object?unproxiedEntity?=?((SessionImplementor)?session)
?????????????????????????.getPersistenceContext()
?????????????????????????.unproxy(proxy);


查看完整回答
反對(duì) 回復(fù) 2019-07-19
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

我編寫了以下代碼,用于從代理中清除對(duì)象(如果它們尚未初始化)

public class PersistenceUtils {

    private static void cleanFromProxies(Object value, List<Object> handledObjects) {
        if ((value != null) && (!isProxy(value)) && !containsTotallyEqual(handledObjects, value)) {
            handledObjects.add(value);
            if (value instanceof Iterable) {
                for (Object item : (Iterable<?>) value) {
                    cleanFromProxies(item, handledObjects);
                }
            } else if (value.getClass().isArray()) {
                for (Object item : (Object[]) value) {
                    cleanFromProxies(item, handledObjects);
                }
            }
            BeanInfo beanInfo = null;
            try {
                beanInfo = Introspector.getBeanInfo(value.getClass());
            } catch (IntrospectionException e) {
                // LOGGER.warn(e.getMessage(), e);
            }
            if (beanInfo != null) {
                for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
                    try {
                        if ((property.getWriteMethod() != null) && (property.getReadMethod() != null)) {
                            Object fieldValue = property.getReadMethod().invoke(value);
                            if (isProxy(fieldValue)) {
                                fieldValue = unproxyObject(fieldValue);
                                property.getWriteMethod().invoke(value, fieldValue);
                            }
                            cleanFromProxies(fieldValue, handledObjects);
                        }
                    } catch (Exception e) {
                        // LOGGER.warn(e.getMessage(), e);
                    }
                }
            }
        }
    }

    public static <T> T cleanFromProxies(T value) {
        T result = unproxyObject(value);
        cleanFromProxies(result, new ArrayList<Object>());
        return result;
    }

    private static boolean containsTotallyEqual(Collection<?> collection, Object value) {
        if (CollectionUtils.isEmpty(collection)) {
            return false;
        }
        for (Object object : collection) {
            if (object == value) {
                return true;
            }
        }
        return false;
    }

    public static boolean isProxy(Object value) {
        if (value == null) {
            return false;
        }
        if ((value instanceof HibernateProxy) || (value instanceof PersistentCollection)) {
            return true;
        }
        return false;
    }

    private static Object unproxyHibernateProxy(HibernateProxy hibernateProxy) {
        Object result = hibernateProxy.writeReplace();
        if (!(result instanceof SerializableProxy)) {
            return result;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    private static <T> T unproxyObject(T object) {
        if (isProxy(object)) {
            if (object instanceof PersistentCollection) {
                PersistentCollection persistentCollection = (PersistentCollection) object;
                return (T) unproxyPersistentCollection(persistentCollection);
            } else if (object instanceof HibernateProxy) {
                HibernateProxy hibernateProxy = (HibernateProxy) object;
                return (T) unproxyHibernateProxy(hibernateProxy);
            } else {
                return null;
            }
        }
        return object;
    }

    private static Object unproxyPersistentCollection(PersistentCollection persistentCollection) {
        if (persistentCollection instanceof PersistentSet) {
            return unproxyPersistentSet((Map<?, ?>) persistentCollection.getStoredSnapshot());
        }
        return persistentCollection.getStoredSnapshot();
    }

    private static <T> Set<T> unproxyPersistentSet(Map<T, ?> persistenceSet) {
        return new LinkedHashSet<T>(persistenceSet.keySet());
    }}

我在RPC服務(wù)的結(jié)果(通過方面)上使用這個(gè)函數(shù),它遞歸地從代理中清除所有結(jié)果對(duì)象(如果沒有初始化它們)。


查看完整回答
反對(duì) 回復(fù) 2019-07-19
  • 3 回答
  • 0 關(guān)注
  • 1647 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)