【问题标题】:Ebean Where with OR (MySQL)Ebean Where 与 OR (MySQL)
【发布时间】:2014-09-23 07:56:46
【问题描述】:

我正在尝试编写一个表达式,它应该给我所有“剪辑”,其中currentUserIdcreator,或者clipsharedWith 给定的currentUserId

creator 是一对多的(一个创作者可以有很多剪辑),sharedWith 是多对多的(许多clips 可以与许多用户共享):

Clip.find.where().or(
      eq("creator.id", currentUserId),
      eq("sharedWith.id", currentUserId)
);

上面只返回与currentUserId共享剪辑的剪辑,因此省略了OR。

我尝试使用 Junctions:

Clip.find.where().disjunction()
                    .add(eq("creator.id", currentUserId))
                    .add(eq("sharedWith.id", currentUserId)).endJunction();

它返回相同的结果。

如果有任何问题,我正在使用 Play Framework 2。

我宁愿避免使用 RawSql,因为这些不是唯一添加的表达式(但是我确实删除了其他表达式以查看它是否有效,因此它没有相关性)。

编辑:

我编写了应该返回正确结果的查询:

select * from clip
join user u on (clip.creator_id = u.id)
left outer join user_clip uc on (clip.id = uc.clip_id)
where (u.id = 1) or (uc.user_id = 1)

如何让 Ebean 为我生成这个?

【问题讨论】:

    标签: java mysql playframework ebean


    【解决方案1】:

    已修复 - 我使用了 raw 表达式:

    Clip.find.where().raw("(sharedWith.id = ?) or (creator.id = ?)", new Object[]{currentUserId, currentUserId});
    

    它生成了以下 SQL:

    select distinct t0.clip_type c0, t0.id c1, t0.clip_type c2, t0.created_at c3, t0.creator_id c4, t0.text c5, t0.image_src c6 
    from clip t0 
    left outer join user_clip t1z_ on t1z_.clip_id = t0.id  
    left outer join user t1 on t1.id = t1z_.user_id  
    where (t1.id = 1) or (t0.creator_id = 1)
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2011-05-07
      • 2015-06-05
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多