java 获取当前对象,java – Hibernate:如何获取当前在会话中的所有对象的列表

Hibernate并没有将其内部公开给公众,所以你不会在公共API中找到你要搜索的内容.但是,您可以在Hibernate接口的实现类中找到答案:

此方法(从

http://code.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 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,然后使用反射来获取所需的私有集合,然后对其进行迭代.

我强烈建议你只做调试代码.这不是公共API,它可能会被新的Hibernate版本制动.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值