7
Good afternoon. I would like to understand how to resolve the following issue:
In the project I’m working on, Backend and Frontend are separated. To persist the data I am using Hibernate and to control the connection with BD I have a filter that opens the connection at the beginning of the request and closes it when returning to Replay.
The use case is: In the frontend the user creates a new user relating to a profile. There are already two profiles registered in the BD being:
1 - BASICO
2 - ADMINISTRADOR
User and Profile are represented by different classes:
User class:
@Entity
public class Usuario{
@Id
private Long id;
private String nome;
@OneToOne
@JoinColumn(name="id_perfil")
private Perfil perfil;
}
Class Profile:
@Entity
public class Perfil{
@Id
private Long id;
private String nome;
}
Example of the User Controller class:
public class UsuarioController{
@PostMapping
public ResponseEntity<Usuario> salva(@RequestBody Usuario usuario) {
usuarioDao.salva(usuario);
return new ResponseEntity<Usuario>(usuario, HttpStatus.CREATED);
}
}
Example of the User class - method to save:
public void salva(Usuario usuario) {
// estou utilizando esta estratégia para gerenciar a conexão no BD
EntityManager manager = JpaUtil.getEntityManager();
manager.persist(usuario);
}
The JSON received from Frontend to register user is:
{
"nome":"usuario novo",
"perfil":{
"id":1
}
}
JSON returned from Backend to Frontend is:
{
"nome":"usuario novo",
"perfil":{
"id":1
"nome":null
}
}
After the user completes the creation of the new user on the screen, the application directs it to the List Users screen. However, in the frontend when including the new line containing the data of the new user in the list, the profile column will appear the value: null - compatible with what is being returned in JSON.
What is good practice for this case???
Backend also return the profile name in JSON, i.e., before returning the User in Sponse, do the following in the controller class: ?
change in User Return Controller - method to create new user
public class Usuariocontroller{
@PostMapping
public ResponseEntity<Usuario> salva(@RequestBody Usuario usuario) {
Usuario usuario = usuarioDao.salva(usuario);
Perfil perfil = perfilDao.perfil(usuario.getPertil().getId());
usuario.setPerfil(perfil);
return new ResponseEntity<Usuario>(usuario, HttpStatus.CREATED);
}
}
Frontend should make a new request by passing the created user ID, so that it receives the full object?
I don’t quite understand the doubt or problem. You’re trying to understand why the profile name is null in the returned json?
– Dherik
Hello Dherik. There are already two profiles registered in the BD being: 1 - BASIC, 2 - ADMINISTRATOR. Since, when creating a user, you don’t need to "create a profile" because it already exists in the BD. When creating a new user, only one relationship is made. Is my doubt clearer? Please reread the session from "What is the good practice for this case???" - Thank you.
– Thiago de Melo Fontana
Use the following relationships HATEOAS would be good practice. In your save you do not need to consult the profile, by default all
*ToOne
isEAGER
, then just give arefresh
.Thepersist
should already return the current state of the managed entity, so try returning the user atsalva
and notvoid
- as Spring Data does, for example.– Bruno César