【问题标题】:@ManyToOne failed to lazy load when Hibernate second cache is enabled@ManyToOne 在启用休眠第二个缓存时无法延迟加载
【发布时间】:2020-05-12 13:08:35
【问题描述】:

我在我的项目中创建了两个非常简单的实体:

@Entity
@Access(FIELD)
public class TestA implements Serializable
{
    @Id
    private UUID id;
    @Version
    private Long hVersion;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private TestB testB;

    // ...
}


@Entity
@Access(FIELD)
public class TestB implements Serializable
{
    @Id
    private UUID id;
    @Version
    private Long hVersion;

    // ...
}

我们有一个可选的 @ManyToOne 从 TestA 到 TestB 的关系。

当我尝试获取 TestA 实例时,如下所示:

entityManager.find(TestA.class, myId);

我有两个选择:一个用于 TestA,另一个用于 TestB,因为它是急切加载的,这不应该发生。

Hibernate: select testa0_.id as id1_20_0_, testa0_.h_version as h_versio2_20_0_, testa0_.test_b_id as test_b_i3_20_0_ from test_a testa0_ where testa0_.id=?
Hibernate: select testb0_.id as id1_21_0_, testb0_.h_version as h_versio2_21_0_ from test_b testb0_ where testb0_.id=?

我尝试了所有这些组合,甚至为了测试将关系设置为非可选:

@ManyToOne(fetch = FetchType.LAZY, optional = true)
private TestB testB;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
private TestB testB;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@LazyToOne(LazyToOneOption.PROXY)
private TestB testB;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@LazyToOne(LazyToOneOption.NO_PROXY)
private TestB testB;

这并没有改变任何东西,TestB 仍然急切地加载。

但是,当我禁用persistence.xml 中的二级缓存时,如下所示:

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.debug" value="false" />
    <property name="hibernate.cache.use_second_level_cache" value="false" />
    <property name="hibernate.cache.use_query_cache" value="false" />
</properties>

现在 TestB 是延迟加载的,只有在访问 TestA.getTestB() 时才会看到第二个选择查询。

当我查看日志时,我可以看到,启用二级缓存后,Hibernate 解析 testB 以将其放入缓存:

DEBUG [org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl] (default task-4) Hibernate RegisteredSynchronization successfully registered with JTA platform
DEBUG [org.hibernate.SQL] (default task-4) select testa0_.id as id1_20_0_, testa0_.h_version as h_versio2_20_0_, testa0_.test_b_id as test_b_i3_20_0_ from test_a testa0_ where testa0_.id=?
DEBUG [org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl] (default task-4) Starting ResultSet row #0
DEBUG [org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl] (default task-4) On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
DEBUG [org.hibernate.engine.internal.TwoPhaseLoad] (default task-4) Resolving associations for [com.monde3.lpt.veriqualis.model.test.TestA#00000000-9999-1111-1111-000000000001]
DEBUG [org.hibernate.engine.internal.TwoPhaseLoad] (default task-4) Adding entity to second-level cache: [com.monde3.lpt.veriqualis.model.test.TestA#00000000-9999-1111-1111-000000000001]
DEBUG [org.hibernate.internal.SessionImpl] (default task-4) Initializing proxy: [com.monde3.lpt.veriqualis.model.test.TestB#00000000-9999-1111-1111-000000000002]
DEBUG [org.hibernate.SQL] (default task-4) select testb0_.id as id1_21_0_, testb0_.h_version as h_versio2_21_0_ from test_b testb0_ where testb0_.id=?
DEBUG [org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl] (default task-4) Starting ResultSet row #0
DEBUG [org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl] (default task-4) On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
DEBUG [org.hibernate.engine.internal.TwoPhaseLoad] (default task-4) Resolving associations for [com.monde3.lpt.veriqualis.model.test.TestB#00000000-9999-1111-1111-000000000002]
DEBUG [org.hibernate.engine.internal.TwoPhaseLoad] (default task-4) Adding entity to second-level cache: [com.monde3.lpt.veriqualis.model.test.TestB#00000000-9999-1111-1111-000000000002]
DEBUG [org.hibernate.engine.internal.TwoPhaseLoad] (default task-4) Done materializing entity [com.monde3.lpt.veriqualis.model.test.TestB#00000000-9999-1111-1111-000000000002]
DEBUG [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl] (default task-4) Skipping aggressive release due to registered resources
DEBUG [org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader] (default task-4) Done entity load : com.monde3.lpt.veriqualis.model.test.TestB#00000000-9999-1111-1111-000000000002

我在这里绝对无助,因为我无法解释这种行为。我的代码中是否存在错误或不良做法?

【问题讨论】:

    标签: java hibernate wildfly lazy-loading many-to-one


    【解决方案1】:

    我找到了!

    为了完全符合 JPA,当访问代理的任何字段时,应该初始化代理,甚至是标识符(Hibernate 不是这样,以避免查询循环)。 p>

    由于Hibernate 5.2.13,有一个新的选项来满足这个规则,hibernate.jpa.compliance.proxy,它默认设置为false,以保持与Hibernate之前的行为一致。

    但是!由于Wildfly 14,此选项在服务器上下文中默认设置为true。我的代码是在 WF18 实例上运行的,所以是一样的。

    解决方案是,如this other question 中所述,覆盖persistence.xml 中的属性,将其重置为默认值false

    <property name="hibernate.jpa.compliance.proxy" value="false" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多