0
Good afternoon, everyone,
I have a model called User that has a permission list, example:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String nome;
@NotNull
private String email;
@NotNull
private String senha;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "usuario_permissao", joinColumns = @JoinColumn(name = "usuario_id"),
inverseJoinColumns = @JoinColumn(name = "permissao_id"))
private Set<Permissao> permissoes;
When starting the project, an SQL is run populating some tables, including the user with their permissions.
My question is, how do I add new permissions in the User table without deleting the data that are already in the permissions list, at the moment I pass a json with only 1 new permission and with that it replaces what already has in the list.
User service
public Usuario updateAcess(Long id, Usuario usuario) {
Usuario usuarioSalvo = findById(id);
BeanUtils.copyProperties(usuario, usuarioSalvo, "id", "nome", "email", "senha");
return usuarioRepository.save(usuarioSalvo);
}
User resource
@PutMapping("/acesso/{id}")
@PreAuthorize("hasAuthority('ROLE_USUARIO_ATUALIZAR') and #oauth2.hasScope('write')")
public ResponseEntity<Usuario> updateAcess(@PathVariable Long id, @Valid @RequestBody Usuario usuario) {
Usuario usuarioSalvo = usuarioService.updateAcess(id, usuario);
return ResponseEntity.ok(usuarioSalvo);
}
JSON Antigo
{
"id": 2,
"nome": "Usuario",
"email": "email",
"senha": "senha",
"permissoes": [
{
"id": 7,
"descricao": "ROLE_DESPESA_DELETAR"
},
{
"id": 9,
"descricao": "ROLE_CONTARECEBER_CRIAR"
}
]}
JSON Novo
{
"id": 2,
"nome": "Usuario",
"email": "email",
"senha": "senha",
"permissoes": [
{
"id": 3,
"descricao": "ROLE_RECEITA_DELETAR"
}
]}
I’m using Flyway even for the creation of tables.
– Murylo Capucho
Then just create a new script for inserting the new permissions
– Weslley Barbosa
In the question you did not mention using flyway, nor other versioning
– Weslley Barbosa
More I want to do this insertion through the front end, more I am testing in Postman, passing a Json, with the user information and with a permission in the list, with this, it erases the old permissions.
– Murylo Capucho
https://javabeginnerstutorial.com/spring-boot-flyway-exampleversion orientation/
– Weslley Barbosa
I understand, so the relationship is incorrect
– Weslley Barbosa
The problem is here, Cascade = Cascadetype.ALL
– Weslley Barbosa
You persist the permissions for the user, Cascade.ALL adds all persistence and holds it to the parent entity, I suggest you change Cascade to = Cascade.PERSIST . So it will only register new permissions if it does not exist in the bank
– Weslley Barbosa
It hasn’t worked yet, I’ll edit the question with the result.
– Murylo Capucho
Try to use more than one Cascade with what you will actually use, because Cascade.ALL includes Cascade.REMOVE, which, if you withdraw a permission, it will delete it
– Weslley Barbosa
All are removing the old and adding only the new to the list.
– Murylo Capucho
Let’s go continue this discussion in chat.
– Weslley Barbosa