Correct way to send data to a view with jpa spring framework

1

What is the best practice to send data to a view with Spring framework, it is correct to send an entity to a view or first convert an entity to a disc.

This is because of the security issue, that not all the data in a table is necessary to send to the view.

Example: Vacancy Entity

@Id
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@Column(name = "nombre")
private String nombre;

@ManyToOne(optional = false)
private User idUsuario;
@Basic(optional = false)

User Entity:

@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@Column(name = "nombre")
private String nombre;

and in the view simply bring the data like this:

Vacante.getId();

Vacante.getIdUsuario().getNombre();

Or create a data with the data that I just want to bring to the view and PASS THE DATA FROM THE ENTITY TO THE DEPT AND send this data to the view and not the entity.

As an additional piece of information I am using Vaadin .

    
asked by Sergio Hilerio 20.11.2017 в 22:40
source

3 answers

2

Never connect an entity directly to a view, it is a bad practice for many reasons. To begin with, an entity is an object of your domain with persistence and when you use it you never know when you are opening connections to the database that you do not want to open (it is not a problem of security but of performance).

Leaving aside the above, there is a more important reason why you should use DTO's. Maybe at this moment all the data you need in the view is in the entity but it may happen that in the future your requirements change and you have to add a new field that is not in database, if you had a discount then add the new field and ready. In an entity it is never a good practice to add a field that does not correspond to a database column ...

More reasons, if you are going to create an api and you want to document it (using swagger for example), it is normal to connect your output to swagger to show you your output in the documentation of the api, with a discount you can control the fields that you show, if you had an entity and you would not be showing everything in your database (bad practice again) ...

If you connect the entities to the view, the program will probably work but when you realize these problems or others arise and you have to change all your views it will be very complicated ...

    
answered by 21.11.2017 / 10:02
source
0

The use of dtos only makes the code more complex. The idea is that you can use the entities to transfer the information between layers. In case your application uses an integration layer as a web service or rest if it is convenient to use a data transfer object. In Spring MVC entities are evaluated on the server so what you do not use is not exposed to the client.

    
answered by 21.11.2017 в 04:29
0

You are in SpringMVC, so the ideal would be to have a controlling class which is responsible for supporting the view.

By experience and seeking to have the most modular and maintainable code possible, this type of architecture will be very useful in the future, giving you a very good level of high cohesion and low coupling.

The controlling class will use the services you need, for example, User Service and Vacant Service and it is these services that will use the DTO (userDTO), creating them when the DAO of the corresponding entity is invoked and it returns the information of that entity.

This additional structure gives you the ability to invoke several DAOs that you need to support sight requests. As you see your controlling class has no knowledge of the DAOs or business entities, is limited to call the services for them to provide the necessary information.

Then there would be the DAO layer which is in charge of managing the entities with your BBDD.

Lastly, the entities would be.

  

Your code remains divided into the following layers:

     

Controller

     

Services - Services.DTOs

     

DAO

     

Entities

And yes, the idea is to use DTOs for performance issues and for all the aspects that @Blazerg mentioned.

    
answered by 23.12.2017 в 01:00