Spring Data Rest 更新导致交叉连接 SQL 错误。

15

我希望使用Spring Data Rest来更新特定用户的行,但在运行时,这个查询会添加奇怪的“交叉连接”到查询中。

Spring Data Rest方法

 @Modifying
@Transactional
@Query("Update Notification n SET n.noticed = true Where n.notificationPost.owner.userId = 1 ")
public void postNoticed();

运行时创建的查询

Hibernate: update notification cross join  set noticed=true where owner_id=?

我唯一的担忧是为什么加入了“cross join”会导致SQL错误

org.postgresql.util.PSQLException: ERROR: syntax error at or near "cross"

我直接通过REST调用以及从MVC控制器中调用这种方法,但两种方式都产生了相同的错误。

提前感谢。


请展示一些实体的代码片段。 - Blank
1个回答

18

按照http://forum.spring.io/forum/spring-projects/data/114271-spring-data-jpa-modifying-query-failure中所述解决方法找到了解决方案。

“批量HQL查询中不能指定任何联接(隐式或显式)。可以在where子句中使用子查询,其中子查询本身可能包含联接。”(Hibernate文档参考:http://docs.jboss.org/hibernate/core.../#batch-direct

因此,我编辑了我的代码以使用子查询。

@Modifying
@Transactional
@Query("Update Notification n SET n.noticed = true Where n.notificationPost.postId in (SELECT n2.notificationPost.postId FROM Notification n2  where n2.notificationPost.owner.userId =:#{#security.principal.user.userId}) ")
public int postNoticed();

仅适用于 DML 类型的操作。 - Alex78191
3
两个链接都失效了;c - Klesun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接