我有一个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.
在说什么。记录确实从数据库中删除,但这个异常导致一切都崩溃了。
有人可以告诉我我在这里做错了什么吗?