I have the following table, currently I can update the User object to add or remove a role, but I cannot add a role to a new user when it is being created (User's roles are added on the same form on the UI) because the intermediate table User_Role is expecting a User Obj for his ID, so naturally on insert I get a User.User_ID = ?.
我有下表,目前我可以更新User对象以添加或删除角色,但我无法在创建新用户时将其添加到新用户(用户的角色添加到UI上的同一表单上),因为中间表User_Role期望用户Obj获取他的ID,因此插入时自然会得到User.User_ID = ?.
Is there any common way to solve this? I was thinking on perhaps trying to get the ID of the inserted user after Save and then calling my update function. I didn't want to to this because I am concerned about performance, specially in the future when bulk uploads may happen.
有什么常见的方法可以解决这个问题吗?我在考虑尝试在Save之后获取插入用户的ID然后调用我的更新函数。我不想这样做因为我关注性能,特别是将来批量上传可能会发生。
I am using CrudRepository on my userRepository.
我在我的userRepository上使用CrudRepository。
if(userRequest.getUser().getIdUser() <= -1){
newUser.setIdUser(0);
newUser.setIsActiveUs(true);
newUser.setDateOfJoin(getCurrentDate());
nuser = usersRepository.save(newUser);
//Get created user and send it to update() with the roleList?
}
1 个解决方案
#1
0
is there any data in UserRole that has any value or is it just a xref? I'd probably just get rid of UserRole altogether, create a one to many or one to one mapping from user --> role, and create a shared auto_increment id. That way you can just cascade the data to role on insert to user - all in one transaction.
UserRole中是否有任何数据具有任何值或者它只是一个外部参照?我可能只是完全摆脱UserRole,从用户 - >角色创建一对多或一对一的映射,并创建一个共享的auto_increment id。这样,您可以将数据级联到插入到用户的角色 - 所有这些都在一个事务中。
#1
0
is there any data in UserRole that has any value or is it just a xref? I'd probably just get rid of UserRole altogether, create a one to many or one to one mapping from user --> role, and create a shared auto_increment id. That way you can just cascade the data to role on insert to user - all in one transaction.
UserRole中是否有任何数据具有任何值或者它只是一个外部参照?我可能只是完全摆脱UserRole,从用户 - >角色创建一对多或一对一的映射,并创建一个共享的auto_increment id。这样,您可以将数据级联到插入到用户的角色 - 所有这些都在一个事务中。