我正在使用Hibernate 4.2,并且我有一个包含子实体集合的父实体(一对多,获取类型为LAZY并带有注释@BatchSize(size=100)).
如果我查询并加载几个父实体并调用访问包含子对象的集合,则hibernate使用@BatchSize预期的.但是如果我调用session,flush然后执行相同的操作,它只为该特定父实体初始化集合.
这是Hibernate预期的行为吗?
编辑:样本
List parents = criteria.list()
parents.get(0).getXs().get(0) // triggers loading Xs of all parents
VS
List parents = criteria.list()
session.flush()
parents.get(0).getXs().get(0) // triggers loading Xs of only the first parent
我将回答我自己的问题,因为我认为这会对其他人有所帮助。我认为这是 Hibernate 的行为,尽管任何文档中都没有提到。当我们调用 Session.flush 时,它会调用 Flushing 事件监听器,我在 AbstractFlushingEventListenrner 类中找到了这段代码
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Post-flushing section
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* 1. Recreate the collection key -> collection map
* 2. rebuild the collection entries
* 3. call Interceptor.postFlush()
*/
protected void postFlush(SessionImplementor session) throws HibernateException {
LOG.trace( "Post flush" );
final PersistenceContext persistenceContext = session.getPersistenceContext();
persistenceContext.getCollectionsByKey().clear();
// the database has changed now, so the subselect results need to be invalidated
// the batch fetching queues should also be cleared - especially the collection batch fetching one
persistenceContext.getBatchFetchQueue().clear();
Run Code Online (Sandbox Code Playgroud)
所以最后一行清除当前上下文的 BatchFetchQueue
| 归档时间: |
|
| 查看次数: |
551 次 |
| 最近记录: |