【问题标题】:QueryDsl fetch lazy propertyQueryDsl 获取惰性属性
【发布时间】:2021-02-08 20:55:55
【问题描述】:

我有一个通常被懒惰地获取的属性:

@Entity
class Version(...,
              @Basic(fetch = FetchType.LAZY)
              @Type(type = "text")
              var mappings: String? = null) {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val id = 0
}

但有时我需要急切地获取它。如何使用 QueryDsl 做到这一点? 这是我目前拥有的:

JPAQuery<Any>(entityManager).from(QVersion.version)
        .where(...)
        .select(QVersion.version)
        .fetchOne()

但是当我稍后尝试访问该属性时,这会导致异常:

org.hibernate.LazyInitializationException: 无法执行请求的延迟初始化 [Version.mappings] - 没有会话并且设置不允许在会话之外加载

【问题讨论】:

  • 我相信您已经配置了字节码增强功能,以便能够延迟加载普通字段(不是实体关系)。我可能完全错了;但是这样做的全部目的是确保该字段是延迟加载的。如果您有时需要急切地加载它;我建议将该字段提取到一个单独的实体(当然具有相同的表和Version 的ID),即VersionMappings,并使用实体关系而不是字段。所以你可以像from(QVersion.version).innerJoin(QVersion.version.mappings).fetchJoin().select(QVersion.version) 那样急切地加载惰性实体关系。
  • @feanor07 是的,字节码增强已开启。这听起来像是一个很好的解决方案。你能用这两个实体的例子把它变成一个答案吗?我在问题中的版本类中添加了一个简单的 id。
  • 我会在几个小时后为你添加一个工作示例;但它将是 Java 而不是 kotlin;我希望这很好:D
  • 当然,我可以自己翻译成 kotlin。我已经尝试了你所说的,但没有得到它的工作,所以期待这个例子。

标签: hibernate kotlin jpa querydsl


【解决方案1】:

我建议将表拆分为两个单独的实体并定义lazy OneToOne 关系,以便您可以在需要时通过QueryDSL 急切地获取它。因此,对于您的示例,您可能有以下实体:

@Entity
@Table(name="version")
class Version{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val id = 0;
    @OneToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id")
    private VersionMappings mappings;
}

@Entity
@Table(name="version")
VersionMappings{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val id = 0;
    @Basic(fetch = FetchType.LAZY)
    @Type(type = "text")
    var mappings: String? = null);
}

如果语法错误,请见谅;因为我不熟悉Kotlin。然后你可以像下面这样急切地获取:

    QVersion version = QVersion.version;

    Version versionWithEagerlyFetchedMapping = JPAQuery<>(entityManager).from(version).where(version.id.eq(id)).rightJoin(version.versionMapping).fetchJoin().select(version).fetchOne();

我准备关注github repo 来展示我的建议。该 repo 是一个带有 3 个独立端点的 Spring Boot 应用程序:

  1. /books-without-author/{id} ==> 获取没有作者的书
  2. /books-with-author-exception/{id} ==> 尝试与作者一起获取图书,但由于无法加载代理的事务而引发异常
  3. /books-with-author/{id} ==> 根据我上面的建议,通过 QueryDSL 急切地获取作者的书。

您可以查看控制台日志以查看 Eager fetch 的工作情况。当您调用上面的第三个端点时,您将看到生成以下查询: select book0_.id as id1_0_0_, bookauthor1_.id as id1_0_1_, book0_.name as name2_0_0_, book0_.publish_year as publish_3_0_0_, book0_.version as version4_0_0_, bookauthor1_.author as author5_0_1_ from book book0_ right outer join book bookauthor1_ on book0_.id=bookauthor1_.id where book0_.id=?

另一方面,第一个端点会生成类似select book0_.id as id1_0_0_, book0_.name as name2_0_0_, book0_.publish_year as publish_3_0_0_, book0_.version as version4_0_0_ from book book0_ where book0_.id=?的查询

【讨论】:

  • 如何插入这个模型?如果我尝试 store 带有 VersionMappings 的版本,hibernate 会尝试插入具有不同唯一 ID 的两行。
【解决方案2】:

对此有一个非常简单的解决方案:.fetchAll()

JPAQuery<Any>(entityManager).from(QVersion.version)
        .fetchAll()  // <- here
        .where(...)
        .select(QVersion.version)
        .fetchOne()

相关hibernate documentation:

如果您正在使用属性级别的延迟获取(使用字节码检测),则可以强制 Hibernate 立即使用 fetch all properties 在第一个查询中获取延迟属性。

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2012-11-02
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多