D
D
dasdasdsadas2022-03-18 14:14:43
Java
dasdasdsadas, 2022-03-18 14:14:43

How to fill fields correctly for Entity when converting from DTO?

I get DTO via API. Next, I convert to entity, but where can I get the values ​​\u200b\u200bof those fields that were not in the request? How to solve this problem correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2022-03-19
@dasdasdsadas

Good afternoon!
I think that the question is formulated incorrectly or perhaps I misunderstood it.
So, as I understand you, you want обновитьan existing entry based on the data that came running from the controller in the form of a DTO.
Firstly, in this case it is worth using PutMapping (full updates) or PatchMapping (partial updates)
Read in detail here:
https://www.baeldung.com/http-put-patch-difference...
Secondly, after , as you got the DTO, you need to map it to the Entity and update it in the database. However, there might be fields that weren't populated in the DTO and you don't want them to be null, right?

Next, I convert to entity, but where can I get the values ​​\u200b\u200bof those fields that were not in the request?

You don't need to pad the fields of the converted entity. You need to:
1) find this entity by ID.
2) If you do not want null fields to be applied to the entity, then when mapping (see below), do a null check and if the DTO field is null, then do not set it to the entity.
3) save the essence.
Regarding mapping:
1) You can use the Converter<S, T>. And for each of the fields, do a type check (pseudocode):
if (fieldValue != null) { entity.setField(fieldValue); }

2) If you are using any mapping library (modelMapper, MapStruct, etc.), then use the capabilities of either. For example, for modelMapper:
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

https://stackoverflow.com/questions/45451025/how-t...
Also commenting on your comments from another answer:
I'm wondering how this issue is resolved. Is it normal that for every request to the service that comes in, you have to go to the database? And this is without logic!

You can set up caching and then there will be no access to the database.

This is the basic situation that everyone faces. Let's say I have an entity Person with a date of birth attribute that I don't want to display in PersonDto. When the user sends me a PersonDto via the API for, say, an update operation, how can I return this date of birth when converting to an entity so that I can save the new entity?

Note that there can be multiple DTOs.
For example,
PersonCreationRequest - dto, which is filled from the front when the user
is created PersonCreationResponse - dto, which is filled from the backend after the user
is created PersonUpdatingRequest - dto, which is filled from the front when the user is updated
PersonUpdatingResponse - dto, which is filled from the backend after the user is updated
PersonDto - the total dto , which is filled from the backend for other needs.
There may be other DTOs...
Accordingly, the answer to your question is to use different DTOs.
I don't know how accurately I could answer your question.

M
Michael, 2022-03-18
@Akela_wolf

Who knows but you? Maybe you have reasonable defaults. Maybe you have some kind of data source (settings, templates, etc.). Maybe this is a general error situation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question