有两个表:bb_players和bb_player_skills。 玩家技能表与bb_players有一对一的关系,也是bb_players的外键。
执行此代码时出错:
Query q = em.createNamedQuery(PlayerSkill.DELETE_SKILL_BY_PLAYER_ID);
q.setParameter("playerID", playerID);
q.executeUpdate();
命名查询是:
@NamedQuery(name = PlayerSkill.DELETE_SKILL_BY_PLAYER_ID, query = "DELETE FROM PlayerSkill s " +
" WHERE s.player.id = :playerID")
postgresql日志的错误是:
ERROR,42601,"syntax error at or near ""cross""",,,,,,
"delete from bb_player_skills cross join bb_players player1_ where id=$1",30,,""
我的命名查询是否错误,我应该如何重写?
答案 0 :(得分:5)
根据您的Hibernate版本,这可能是一个开放的Hibernate问题。
来自:https://hibernate.atlassian.net/browse/HHH-7314
在需要通过Hibernate实体连接加入的条件下使用JPA Delete查询会为PostgreSQL生成无效的SQL。 PostgreSQL不能在DELETE查询的FROM子句中使用CROSS JOIN。
答案 1 :(得分:0)
看起来它不是一对一的关系。从bb_players来看,它是一对多关系,从bb_player_skills它应该是多对一的关系。
答案 2 :(得分:0)
万一其他人遇到相同的问题,这是一个解决方法。
可以重写命名查询以避免连接交叉。
@NamedQuery(name = "PlayerSkill.DELETE_SKILL_BY_PLAYER_ID", query = "DELETE FROM PlayerSkill s " +
" WHERE s.player IN (SELECT player FROM Player player where player.id=:playerID)")