如何将 SQL 查询转换为 JPQL
How to translate SQL query to JPQL
我正在尝试将 SQL 查询转换为 JPQL,但找不到解决方案。
SELECT * FROM event
INNER JOIN users_events ON users_events.event_id = event.id
INNER JOIN user ON users_events.user_id = SOME_USER_ID
LEFT JOIN pool_option ON pool_option.event_id = event.id
LEFT JOIN vote ON vote.pool_option_id = pool_option.id
GROUP BY event.id
HAVING vote.id is null;
我写这个异常的 JPQL:
java.sql.SQLSyntaxErrorException: Unknown column 'votes4_.id' in 'having clause'
TypedQuery<Event> query = entityManager.createQuery("SELECT e FROM Event e join e.users u left join e.poolOptions po left join po.votes v " +
"WHERE u.email = :email GROUP BY e.id HAVING v.id is null", Event.class);
投票肯定具有属性 "id" 并且 SQL 查询在 MySQL 中有效,但 JPQL 不起作用。
为了使 HAVING v.id
正常工作,您需要将此列添加到 select 子句中。请尝试:
TypedQuery<Event> query = entityManager.createQuery("SELECT e, v.id FROM Event e join e.users u left join e.poolOptions po left join po.votes v WHERE u.email = :email GROUP BY e.id HAVING v.id is null", Event.class);
我正在尝试将 SQL 查询转换为 JPQL,但找不到解决方案。
SELECT * FROM event
INNER JOIN users_events ON users_events.event_id = event.id
INNER JOIN user ON users_events.user_id = SOME_USER_ID
LEFT JOIN pool_option ON pool_option.event_id = event.id
LEFT JOIN vote ON vote.pool_option_id = pool_option.id
GROUP BY event.id
HAVING vote.id is null;
我写这个异常的 JPQL:
java.sql.SQLSyntaxErrorException: Unknown column 'votes4_.id' in 'having clause'
TypedQuery<Event> query = entityManager.createQuery("SELECT e FROM Event e join e.users u left join e.poolOptions po left join po.votes v " +
"WHERE u.email = :email GROUP BY e.id HAVING v.id is null", Event.class);
投票肯定具有属性 "id" 并且 SQL 查询在 MySQL 中有效,但 JPQL 不起作用。
为了使 HAVING v.id
正常工作,您需要将此列添加到 select 子句中。请尝试:
TypedQuery<Event> query = entityManager.createQuery("SELECT e, v.id FROM Event e join e.users u left join e.poolOptions po left join po.votes v WHERE u.email = :email GROUP BY e.id HAVING v.id is null", Event.class);