3
I’m having trouble deleting a @Manytomany relationship on JPA. In short, I have 3 tables: USER, PERMISSION and USUARIO_PERMISSAO which is the relationship of the previous ones, the relationship is N to N. The problem is that I do not know how to do to remove or add a new permission to a user existing on the basis, follows simplified model:
@Entity
@Table(name="TB_USUARIO")
public class Usuario {
@Id
@Column(name="NO_USUARIO", unique=true, nullable=false, length=50)
private String noUsuario;
@ManyToMany
@JoinTable(
name="TB_USUARIO_PERMISSAO"
, joinColumns={
@JoinColumn(name="NO_USUARIO", nullable=false)
}
, inverseJoinColumns={
@JoinColumn(name="NO_METODO", referencedColumnName="NO_METODO", nullable=false),
@JoinColumn(name="NO_PERMISSAO", referencedColumnName="NO_PERMISSAO", nullable=false))
}
)
private List<Permissao> permissoes;
}
@Entity
@Table(name="TB_PERMISSAO")
public class Permissao {
@EmbeddedId
private PermissaoPK id;
@ManyToMany(mappedBy="permissoes")
private List<Usuario> usuarios;
}
My problem is with field mapping permissions class User, when I insert a new User into the database already adding the permission works, follow code that works:
Usuario usuario = new Usuario("USUARIO_01");
usuario.getPermissoes().add(permissao); //permissao ja cadastrada no banco
entityManager.persist(usuario);
This code correctly inserts a new user and also the relationship with the permission in the table TB_USUARIO_PERMISSAO. The problem now is to delete permissions, let’s assume that a user has 2 permissions and I want to delete one of them, how do I do that? It follows that doesn’t work:
Usuario usuario = entityManager.find(Usuario.class, "USUARIO_01");
usuario.getPermissoes().remove(0);
entityManager.merge(usuario);
This code above does not work, do I have to change the mapping, I know there, maybe creating a user entity permission? The mapping is the way the reverse engineering engine created it.. If I remove a user, the permissions are automatically deleted. My problem is in rule out and also add a new permission of a user already registered in the bank. I have also used Cascade and unsuccessfully.
Can help?
Note: To simplify changing the code by the editor, there may be an error that if you put it in the IDE it will not compile, the idea is to show the base. I also tried other ways to remove/add that I thought it best not to mention since they didn’t work
Grateful!
"this code does not work": gives some error or simply does not remove the relationship?
– Bruno César
You want to remove only one of the permissions, but aren’t they independent of each other and its users? The user only makes use of this existing permission, when deleting a user the relationship with this permission goes missing, but the permission still exists to be used by other users, I got it wrong?
– dougg0k
@Brunswith no error, simply no action is executed.. I am with active Iberianate debugging and really does nothing.
– Welmau
@Douglasgaldino By deleting the user only the permissions relationship (tb_usuario_permission) with the user is deleted. When I say that I cannot remove a user permission, I mean modify only the tb_usuario_permissao relationship table, i.e., tb_permissao at no time should be changed.
– Welmau
@wchagas got it. It includes an answer with what was possible to understand, as it is not a completely executable example inferred some things in order to work. See if it fits, if not, include more details :)
– Bruno César
@Brunocésar thanks Runo, I’ll analyze!
– Welmau
If you try to remove all relations relating to a particular user, it works normally?
– dougg0k
You are not adding the user to the permission, you are only adding the permission to the user. See this.
– Sidney