【问题标题】:JPA find grandparent entity given only child unique keyJPA仅给定子唯一键查找祖父母实体
【发布时间】:2018-07-23 01:20:08
【问题描述】:

我正在使用 JPA 和 MySQL。

如果我有一堆相关的实体如下:

  • Grandparent_entity 有许多 Parent_entity (OneToMany)。坚持使用连接表。
  • Parent_entity 有一个 Child_entity (OneToOne)。在父表中持续使用 child_uuid。

如果我只获得子唯一键,我如何获得祖父母实体?

我的想法是一个包含 3 个连接的查询 - 子到父,父到表,表到祖父。这是最好的方法吗?

【问题讨论】:

标签: java mysql hibernate jpa


【解决方案1】:

使用 hql:

FROM grand_parent gp, parent p WHERE p.child.id = :id AND p in (gp.parents)

【讨论】:

    【解决方案2】:

    这行得通:

    select g from Grandparent_entity g join fetch g.Parent_entity p join fetch p.Child_entity c WHERE (c.uniqueKey = :uniueKey)
    

    【讨论】:

      猜你喜欢
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多