我正在尝试编写一个表达式,这个表达式应该给我所有的“剪辑”,其中currentUserId是creator,或者clip是给定的currentUserId。
creator是一对多的(创建者可以有许多剪辑),而sharedWith是多对多的(许多clip可以与许多用户共享):
Clip.find.where().or(
eq("creator.id", currentUserId),
eq("sharedWith.id", currentUserId)
);以上只返回剪辑与currentUserId共享的剪辑,因此省略了OR。
我试着用连接:
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为我生成这个?
发布于 2014-09-23 12:17:35
修正了它-我使用了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)https://stackoverflow.com/questions/25989850
复制相似问题