栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

休眠:如何获取会话中当前所有对象的列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

休眠:如何获取会话中当前所有对象的列表

Hibernate不会向公众公开其内部信息,因此您不会在公共API中找到要搜索的内容。但是,您可以在Hibernate接口的实现类中找到答案:此方法(摘自http://pre.google.com/p/bo2/source/browse/trunk/Bo2ImplHibernate/main/gr/interamerican/bo2/
impl / open / hibernate /
HibernateBo2Utils.java

)将告知会话中是否存在对象:

public static Object getFromSession        (Serializable identifier, Class<?> clazz, Session s) {       String entityName = clazz.getName();    if(identifier == null) {       return null;    }          SessionImplementor sessionImpl = (SessionImplementor) s;    EntityPersister entityPersister = sessionImpl.getFactory().getEntityPersister(entityName);    PersistenceContext persistenceContext = sessionImpl.getPersistenceContext();    EntityKey entityKey = new EntityKey(identifier, entityPersister, EntityMode.POJO);    Object entity = persistenceContext.getEntity(entityKey);    return entity;    }

如果进一步深入研究,您将看到PersistenceContext的唯一实现是org.hibernate.engine.StatefulPersistenceContext。此类具有以下集合:

// Loaded entity instances, by EntityKeyprivate Map entitiesByKey;// Loaded entity instances, by EntityUniqueKeyprivate Map entitiesByUniqueKey;// Identity map of EntityEntry instances, by the entity instanceprivate Map entityEntries;// Entity proxies, by EntityKeyprivate Map proxiesByKey;// Snapshots of current database state for entities// that have *not* been loadedprivate Map entitySnapshotsByKey;// Identity map of array holder ArrayHolder instances, by the array instanceprivate Map arrayHolders;// Identity map of CollectionEntry instances, by the collection wrapperprivate Map collectionEntries;// Collection wrappers, by the CollectionKeyprivate Map collectionsByKey; //key=CollectionKey, value=PersistentCollection// Set of EntityKeys of deleted objectsprivate HashSet nullifiableEntityKeys;// properties that we have tried to load, and not found in the databaseprivate HashSet nullAssociations;// A list of collection wrappers that were instantiating during result set// processing, that we will need to initialize at the end of the queryprivate List nonlazyCollections;// A container for collections we load up when the owning entity is not// yet loaded ... for now, this is purely transient!private Map unownedCollections;// Parent entities cache by their child for cascading// May be empty or not contains all relation private Map parentsByChild;

因此,您需要做的是将PersistenceContext强制转换为StatefulPersistenceContext,然后使用反射来获取所需的私有集合,然后对其进行迭代。

我强烈建议您仅在调试代码上执行此操作。这不是公共API,它可能会因较新的Hibernate版本而制止。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号