【问题标题】:Prevent "n+1 selects" with JPA/Hibernate constructor expression?使用 JPA/Hibernate 构造函数表达式防止“n+1 选择”?
【发布时间】:2012-09-28 17:19:16
【问题描述】:

我有两个实体,ItemData,还有一个 DTO 类 ItemDataItemDataItemData 组成,没有 JPA 映射。为了检索填充的 ItemDatas 列表,我在 JPQL 中使用了构造函数表达式:

select new my.package.ItemData(i, d)
from Item i, Data d
where i.id = d.itemId

这就是 Hibernate 正在做的事情:它不是同时获取 ItemData 的数据,而是先获取它们的 ID,然后在 n 单独的选择语句中获取数据。有没有办法改变这种行为?

Hibernate:
    select
        item0_.id as col_0_0_,
        data1_.id as col_1_0_ 
    from
        ITEM item0_,
        DATA data1_

Hibernate: 
    select
        item0_.no as no1_0_,
        item0_.description as description1_0_,
        item0_.organic as bio1_0_,
        item0_.gluten as gluten1_0_,
        item0_.laktose as laktose1_0_
    from
        ITEM item0_ 
    where
        item0_.id=?

Hibernate: 
    select
        data0_.amount as amount1_3_0_,
        data0_.avg as avg3_0_,
        data0_.total as total3_0_
    from
        DATA data0_ 
    where
        data0_.id=?

Hibernate: 
    select
        item0_.no as no1_0_,
        item0_.description as description1_0_,
        item0_.organic as bio1_0_,
        item0_.gluten as gluten1_0_,
        item0_.laktose as laktose1_0_
    from
        ITEM item0_ 
    where
        item0_.id=?

Hibernate: 
    select
        data0_.amount as amount1_3_0_,
        data0_.avg as avg3_0_,
        data0_.total as total3_0_
    from
        DATA data0_ 
    where
        data0_.id=?

...and so on...        

【问题讨论】:

  • @DataNucleus 为什么要删除 JPA 标签?
  • 仅仅是因为您将问题中的所有内容都表达为特定于休眠的(休眠 SQL 等)。具有 JPA 一般背景的人无法在其中为您提供任何帮助......它的 Hibernate 特定
  • 我不同意。有一些 JPQL 表达式会影响提供者的行为(例如,“join fetch”)。
  • 您需要将 Item 和 Data 映射为 OneToMany 语句,以便 Hibernate 知道有一个 Set 结构在起作用。然后将自动生成连接。
  • 为什么投反对票?我愿意改进...

标签: java hibernate jpa select-n-plus-1


【解决方案1】:

使用左连接获取怎么样?
我承认我没有使用 CTOR 表达式,但是当我需要获取父实体及其子实体时,我使用了 left join fetch,它总是像魅力一样工作。
我只能假设,因为我们在这里处理对象的构造,
Hibernate 不“想要”留下一个“部分构造”的对象(或者说 - 一个处于“惰性评估状态”的对象),并且由于您没有使用“left join fetch”执行“eager fetch”,它执行 N +1 获取:
首先,它获取所有 ID,然后对于每个相关 ID,它执行另一个获取。
阅读有关 left join fetch here 的更多信息(只需在页面上查找“left join fetch”)

【讨论】:

  • 谢谢 - 我可以在构造函数表达式中使用什么东西,就像连接的 fetch 关键字一样?
  • @Mulmoth - 我不知道
【解决方案2】:

这是Blaze-Persistence Entity Views 的完美用例。

我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义模型之间轻松映射,例如 Spring Data Projections on steroids。这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。

我想你的实体模型看起来像这样?

@Entity
class Item {
  @Id Long id;
  String description;
  @OneToMany(mappedBy = "item")
  Data data;
  ...
}
@Entity
class Data {
  @Id Long id;
  @ManyToOne Item item;
  Long amount;
  BigDecimal avg;
  ...
}

使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

@EntityView(Item.class)
public interface ItemData {
    @IdMapping
    Long getNo();
    String getDescription();
    @Mapping("SUM(data.amount)")
    Long getAmount();
}

查询是将实体视图应用于查询的问题,最简单的就是通过 id 进行查询。

ItemData a = entityViewManager.find(entityManager, ItemData.class, id);

Spring Data 集成让您可以像使用 Spring Data Projections 一样使用它:https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

【讨论】:

    猜你喜欢
    • 2012-11-04
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2010-10-24
    • 2011-06-25
    • 2021-01-09
    相关资源
    最近更新 更多