Hibernate:如何获得当前会话中所有对象的列表 [英] Hibernate: how to get a list of all the objects currently in the session

查看:173
本文介绍了Hibernate:如何获得当前会话中所有对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了很好的,老的和令人畏惧的 TransientObjectException ,并且,正如在这种情况下经常发生的那样,我在定位什么样的细微错误时遇到了问题代码导致了这个问题。

I'm getting the good, old and dreaded TransientObjectException, and, as often happens in such case, I'm having problems locating what kind of subtle bug in the code is causing the problem.

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

My question is: is there a way to obtain a list of every object that's in the current Hibernate session?

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

I'll probably have solved the current problem by the time I get an answer for this question, but, anyway, being able to list everything that is the session would help a lot in the next time that happens.

推荐答案

Hibernate不向公众公开它的内部信息,所以你不会在公共API。但是,您可以在Hibernate接口的实现类中找到您的答案:
此方法(取自http://code.google.com/p/bo2/source/browse/trunk/Bo2ImplHibernate/main/gr/interamerican/bo2/ impl / open / hibernate / HibernateBo2Utils.java )会告诉对象是否存在于会话中:

Hibernate does not expose its internals to the public, so you won't find what you are searching for in the public API. However you can find your answer in the implementation classes of the Hibernate interfaces: This method (taken from http://code.google.com/p/bo2/source/browse/trunk/Bo2ImplHibernate/main/gr/interamerican/bo2/impl/open/hibernate/HibernateBo2Utils.java) will tell if an object exists in the 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。
这个类有以下集合:

If you drill down a little more, you will see that the only implementation of PersistenceContext is org.hibernate.engine.StatefulPersistenceContext. This class has the following collections:

// 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,然后使用反射来获取私人收集,然后迭代它。

So, what you need to do is cast the PersistenceContext to a StatefulPersistenceContext, then use reflection to get the private collection that you want and then iterate on it.

我强烈建议您只在调试代码时执行此操作。这不是公共API,它可能会阻碍新版本的Hibernate。

I strongly suggest you do that only on debugging code. This is not public API and it could brake by newer releases of Hibernate.

这篇关于Hibernate:如何获得当前会话中所有对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆