春季数据 JPA 删除本机查询抛出异常
2022-09-01 18:16:59
我有一个实体和一个实体。该关系定义如下:User
Role
@OneToMany
@JoinTable(name="USER_ROLES", inverseJoinColumns=@JoinColumn(name="ROLE_ID"))
private List<Role> roles = null;
现在,当我删除某个角色时,我需要从具有该角色的所有用户中删除该角色。通常,您可以通过查找具有此角色的所有用户,从列表中删除该角色并保存该用户来执行类似操作。但是,当可能有超过一百万用户时,我不想在应用程序中循环访问这么多实体。因此,我想使用本机查询从联接表中删除行。我尝试将其添加到我的存储库中:USER_ROLES
@Query(value="DELETE FROM user_roles WHERE role_id = ?1", nativeQuery=true)
public void deleteRoleFromUsersWithRole(Long roleId);
但是,当我执行此操作时,我在日志中看到以下内容:
[EL Fine]: sql: 2013-11-02 14:27:14.418--ClientSession(707349235)--Connection(2096606500)--Thread(Thread[http-bio-8080-exec-4,5,main])--DELETE FROM user_roles WHERE role_id = ?
bind => [1000110139999999953]
[EL Fine]: sql: 2013-11-02 14:27:14.478--ClientSession(707349235)--Thread(Thread[http-bio-8080-exec-4,5,main])--SELECT 1
[EL Warning]: 2013-11-02 14:27:14.482--UnitOfWork(1795045370)--Thread(Thread[http-bio-8080-exec-4,5,main])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: No results were returned by the query.
Error Code: 0
Call: DELETE FROM user_roles WHERE role_id = ?
bind => [1000110139999999953]
Query: DataReadQuery(sql="DELETE FROM user_roles WHERE role_id = ?")
我不明白在说什么。记录确实会从数据库中删除,但此异常会导致所有内容都爆炸。No results were returned by the query.
有人可以告诉我我在这里做错了什么吗?