我想在JPA中加入3个表的联接。 这是3张桌子:
USER [(pk)userId,登录名和密码];
任务[(pk)taskId,任务名,开始,结束,#userId];
TASK_USER [(cmp)taskId,(cmp)userId];
这是要在JPA中执行的SQL查询:
SELECT tu.taskId, t.start, t.end
FROM task as t,
user as u,
task_user as tu
WHERE (tu.taskId = t.taskId
AND tu.userId = u.userId
AND t.userId = u.userId)
AND tu.userId = '2';
但是给我带来问题的是应JPA请求编写的语法。
你能帮我吗?
答案 0 :(得分:0)
尝试一下:
SELECT tu.taskId, t.start, t.end FROM task as t inner join user as u
on u.userId = t.userId inner join task_user as tu on tu.taskId = t.taskId
where tu.userId = '2';
答案 1 :(得分:0)
SELECT tu.taskId, t.start, t.end
FROM task as t, user as u, task_user as tu
WHERE tu.taskId = t.taskId AND tu.userId = u.userId
AND t.userId = u.userId
AND tu.userId = '2';
答案 2 :(得分:0)
如果您已使用ManyToMany关系定义了实体之间的关系 如果不是https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/
,请检查此内容您可以编写这种查询
SELECT t.taskId, t.start, t.end FROM User as u left join u.tasks WHERE u.userId = '2';
答案 3 :(得分:0)
谢谢,没关系。我可以在JPA中将查询编写为SQL。语法与SQL相同,我可以返回一个对象作为示例:join中的用户对象