代码之家  ›  专栏  ›  技术社区  ›  Archimedes Trajano

如何在JPQL中执行紧急获取

  •  0
  • Archimedes Trajano  · 技术社区  · 5 年前

    我有 ContactAddressLink (为简洁起见,去掉了注释)

    class ContactAddressLink {
      Contact contact;
      Address address;
      ... some extra fields ...
    }
    

    所以我有个问题

    select cal from Contact c, Address a, ContactAddressLink cal where
    cal.contact = c and cal.address = a
    

    这给了我我期望的问题。但是,因为我会在看到一堆查询得到每个地址后使用这些地址。

    我想做的是

    select cal eager fetch cal.a from Contact c, Address a, ContactAddressLink cal where
    cal.contact = c and cal.address = a
    

    我记得看到过类似的东西,但我记不清确切的语法。

    0 回复  |  直到 5 年前
        1
  •  2
  •   Ken Chan    5 年前

    是的。你是对的。语法是 [inner|left] join fetch . 例子:

    select cal from ContactAddressLink cal
    inner join fetch cal.contact c
    inner join fetch cal.address a
    where cal.id = 123456789
    

    ContactAddressLink 如果有的话 Contact Address ,使用 inner join fetch

    如果你想匹配 联系人地址链接 即使它没有 接触 地址 left join fetch .

        2
  •  0
  •   Dominik    5 年前

    记住按照 JPA Specification @OneToOne @ManyToOne 被热切地吸引。你应该把它改成lazy如果不是,你会一直取那些对象。