【问题标题】:FetchType EAGER is not the same as Root fetch()?FetchType EAGER 和 Root fetch() 不一样?
【发布时间】:2013-06-18 14:29:01
【问题描述】:

我正在使用 Hibernate 4 作为我的提供者,使用 JPA 2 CriteriaBuilder 构建查询。我一直在玩 FetchModes,并得出结论 SELECT 是我需要的。但是 SELECT 强制启用 FetchType.LAZY。我在某处读过这个,似乎就是这样。

在 Devices 类中,此配置会产生正确的结果:

@Fetch(value=FetchMode.SELECT)
@OneToMany(fetch=FetchType.EAGER, mappedBy = "device")
public List<DevInterfaces> getDevInterfaces() {
    return this.devInterfaces;
}

但是,我不希望在我的实体中硬编码 FetchType=EAGER。我想控制提取发生的时间,我想我可以用这样的 fetch() 来做到这一点:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Devices> dq = cb.createQuery(Devices.class);
Root<Devices> dev = dq.from(Devices.class);
dev.fetch(Devices_.devInterfaces)

但结果是内部连接,我不希望这样。

如何在不硬编码 FetchType.EAGER 的情况下使用 FetchMode.SELECT 获取?

【问题讨论】:

    标签: hibernate criteria-api


    【解决方案1】:

    如问题所述,以及here 的记录,fetch 使用内部连接。

    在使用重载的 fetch 方法时可以指定连接类型,该方法也将 JoinType 作为参数:

    dev.fetch(Devices_.devInterfaces, JoinType.LEFT)
    

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多