Answer the question
In order to leave comments, you need to log in
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
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?
Converter<S, T>
. And for each of the fields, do a type check (pseudocode):if (fieldValue != null) { entity.setField(fieldValue); }
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
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!
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?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question