我有一个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.
的含义。记录已经从数据库中删除,但这个异常导致一切都崩溃了。
请问有人可以告诉我在这里哪里出错了吗?
@Transactional
注释。感谢您的帮助。 - dnc253