【问题标题】:Member of in JPQL not translated into SQL exists clauseJPQL 中的成员未翻译成 SQL 存在子句
【发布时间】:2016-06-03 03:03:23
【问题描述】:

为什么JPQL中的member of被翻译成:(?IN(select ...))

我在 JPQL 中创建了以下查询:

select  si
    from  ShoppingItem si
    join  si.owner u
    where  u = :owner
      and  :following member of u.followers" 

上面的查询翻译成:

select  shoppingit0_.id ...
    from  shopping_item shoppingit0_
    inner join  user user1_ on shoppingit0_.owner_id=user1_.id
    where
       and  user1_.id=?
      and  **(? IN (
                SELECT  followers2_.id
                    from  following followers2_
                    where  user1_.id=followers2_.target_id)
             )** ... 

我的技术栈:Spring Data JPA、hibernate、InnoDB 引擎、MySQL DB...

如您所见,成员正在被“翻译”成 (?IN (select follower2_.id from following followers2_ where user1_.id=followers2_.target_id) )

MySQL reference 给出了使用“Exists”查询优化“in”子查询的建议。
我的问题是:为什么将成员转换为 ? in (select ...) 和 not into exists select(...) ?

在这种情况下,是否有可能告诉 hibernate 使用“exists”而不是“in”?

【问题讨论】:

    标签: mysql hibernate jpa innodb spring-data-jpa


    【解决方案1】:

    您可以尝试以下查询:

    select  si
    from  ShoppingItem si
    join  si.owner u
    where  u = :owner
      and exists (select f from Following f where f.target = u and f = :following )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多