gpt4 book ai didi

java - Hibernate @BatchSize 在 session.flush 之后无法按预期工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:29:55 24 4
gpt4 key购买 nike

我正在使用 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     

对比

    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();

因此最后一行清除了当前上下文的 BatchFetchQueue

关于java - Hibernate @BatchSize 在 session.flush 之后无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31744488/

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