How to add registration to the list without harming existing data

Asked

Viewed 54 times

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"
    }
]}

1 answer

0

Well, this is a very common organizational problem in design. An initial version of the database is created, but for some reason it was necessary to change the structure or even expand and add new tables, for this type of problem we can use the Flyway. Its use is extremely simple to understand and easy to use.


What is Flyway?

Flyway is one of several tools that propose to bring order and organization for SQL scripts that are executed in the data, functioning as a version control of the same.

Source: /a/332547/20508

  • I’m using Flyway even for the creation of tables.

  • Then just create a new script for inserting the new permissions

  • In the question you did not mention using flyway, nor other versioning

  • 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.

  • https://javabeginnerstutorial.com/spring-boot-flyway-exampleversion orientation/

  • I understand, so the relationship is incorrect

  • The problem is here, Cascade = Cascadetype.ALL

  • 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

  • It hasn’t worked yet, I’ll edit the question with the result.

  • 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

  • All are removing the old and adding only the new to the list.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.