gpt4 book ai didi

java - Hibernate:如何获取当前 session 中所有对象的列表

转载 作者:太空狗 更新时间:2023-10-29 23:03:12 27 4
gpt4 key购买 nike

我得到了好的、旧的和可怕的 TransientObjectException,而且,正如在这种情况下经常发生的那样,我在定位导致问题的代码中的细微错误类型时遇到了问题。

我的问题是:有没有办法获取当前 Hibernate session 中每个对象的列表?

当我得到这个问题的答案时,我可能已经解决了当前的问题,但是无论如何,能够列出 session 中的所有内容将在下次发生时大有帮助。

最佳答案

Hibernate 不会向公众公开其内部结构,因此您不会在公共(public) API 中找到您要搜索的内容。但是,您可以在 Hibernate 接口(interface)的实现类中找到答案:此方法(取自 http://code.google.com/p/bo2/source/browse/trunk/Bo2ImplHibernate/main/gr/interamerican/bo2/impl/open/hibernate/HibernateBo2Utils.java)将判断 session 中是否存在对象:

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 EntityKey
private Map entitiesByKey;

// Loaded entity instances, by EntityUniqueKey
private Map entitiesByUniqueKey;

// Identity map of EntityEntry instances, by the entity instance
private Map entityEntries;

// Entity proxies, by EntityKey
private Map proxiesByKey;

// Snapshots of current database state for entities
// that have *not* been loaded
private Map entitySnapshotsByKey;

// Identity map of array holder ArrayHolder instances, by the array instance
private Map arrayHolders;

// Identity map of CollectionEntry instances, by the collection wrapper
private Map collectionEntries;

// Collection wrappers, by the CollectionKey
private Map collectionsByKey; //key=CollectionKey, value=PersistentCollection

// Set of EntityKeys of deleted objects
private HashSet nullifiableEntityKeys;

// properties that we have tried to load, and not found in the database
private 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 query
private 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,然后使用反射获取您想要的私有(private)集合,然后对其进行迭代。

我强烈建议您仅在调试代码时这样做。这不是公共(public) API,它可能会因 Hibernate 的较新版本而中断。

关于java - Hibernate:如何获取当前 session 中所有对象的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com