Doubt about mapping with Java

Asked

Viewed 407 times

2

I created a project by putting system authentication including login and password directly in the Java code, but I know this is super wrong, the most recommended for implementation of authentication is the system search login and password direct from the bank, in addition to authentication the user will also need permission, that is, it does not mean that when logging in the user will have direct view of the pages, the rules of navigability will put in Spring Security, what is being complicated for me is to perform entity mapping in the Java classes.

I know how to create the database through SQL, but I don’t know how to abstract it for entity mapping in Java, see the model that build in SQL;

Entity User;

CREATE TABLE usuario (
    codigo BIGINT(20) PRIMARY KEY AUTO_INCREMENT,
    nome VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL,
    senha VARCHAR(120) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Entity Group;

CREATE TABLE grupo (
    codigo BIGINT(20) PRIMARY KEY,
    nome VARCHAR(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Entity Permission;

CREATE TABLE permissao (
    codigo BIGINT(20) PRIMARY KEY,
    nome VARCHAR(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Entity Group surgery;

CREATE TABLE grupopermissao (
    codigo_grupo BIGINT(20) NOT NULL,
    codigo_permissao BIGINT(20) NOT NULL,
    PRIMARY KEY (codigo_grupo, codigo_permissao),
    FOREIGN KEY (codigo_grupo) REFERENCES grupo(codigo),
    FOREIGN KEY (codigo_permissao) REFERENCES permissao(codigo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Entity User group;

CREATE TABLE usuariogrupo (
    codigo_usuario BIGINT(20) NOT NULL,
    codigo_grupo BIGINT(20) NOT NULL,
    PRIMARY KEY (codigo_usuario, codigo_grupo),
    FOREIGN KEY (codigo_usuario) REFERENCES usuario(codigo),
    FOREIGN KEY (codigo_grupo) REFERENCES grupo(codigo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I will make an explanation of my intentions regarding these tables above, the tables would behave as follows.

In the system the user would enter the registration screen permissions and would register only two permissions;

  • VISUALIZAR_CADASTRO
  • VISUALIZAR_PESQUISA

After the user would enter the group registration screen and would register only two groups;

  • Administrators
  • Salesmen

Then the user would enter a registration screen to associate the groups to the permissions, then you imagine you enter a screen with two Combobox, one would list the groups and the other list the permissions, after choosing would only sobmeter the form on that screen.

if administrators were code 1 and sellers were code 2 then permission to register code 1 and view searches if code 2 would look something like this;

Group surgery

  • 1,1
  • 1,2
  • 2,1

This means that administrator would be in the group that has permission to search and register and group of sellers only to register.

I’m sorry about the long post, but it was necessary to understand my context.

My only difficulty is that I don’t know how to map my entities in relation to the reality described above, I don’t even know where to go.

  • Hi wladyband. I don’t know if I understood the doubt right, but to me it seems that you have two relationships @ManyToMany, that is, if you want to handle this with JPA you will have an entity Grupo with a List<Usuario> and a List<Permisao>. On the side of Permisao and of Usuario you will have a List<Grupo>. Note however that if you will use Spring Security, there is no reason to invent the wheel. It has the scheme Group Authorities and the UserDetailsService.

1 answer

1

To map the entities with JPA, simply create a class represented by the entity and the class attributes will represent the columns. I will leave here an example of the mapping of your entity usuario. For other entities, see this guide and it will help you to follow (https://www.tutorialspoint.com/jpa/)

@Entity
@Table(name = "USUARIO")
public class Usuario implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "codigo")
    private Long codigo;

    @Column(name = "EMAIL", nullable = false, unique=true)
    private String email;

    @Column(name = "SENHA", nullable = false)
    private String senha;
}

To implement the different access levels of each user, Spring Security offers a number of validations and helper methods, see this guide (http://www.baeldung.com/role-and-privilege-for-spring-security-registration). Roughly you will need to map the entities that represent the ROLES of each user or user group and will use them as permissions.


Your problem is very common and with a little research on entity mapping with JPA and access restrictions with Spring Security, you will be able to follow.

I hope I helped. I will leave here some links that may be useful.

  • Thanks for the help, what I’m having difficulty is to know how will be the entity groupopermissao and usuariogrupo.

  • 1

    Hi @wladyband, I don’t know if you saw my comment. Like this schema does not have relationship attributes no need to map these tables as entity. You can do the mapping with @ManyToMany and @JoinTable.

Browser other questions tagged

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