-2

I use the Spring framework in my project, I stuck with the identification of DTO to be saved. I have a @RestController PUT-method end-point. I need to implement the logic: if an entity exists than override at DB else create a new one. So if the client will submit DTO 2 times it will 2 duplicated DTOs at DB. The only option I see to identify by ID(Actually, it how Spring Data operates out-of-the-box if id exists then override values of the entity). However, how can I hide this id from the client? Thanks.

P.S: Create a UNIQUE index for all fields and compare DTO by all fields is NOT the solution in my case. Many thanks.

1 Answers1

0

Annote @JsonIgnore on ID for the DTO will solve your problem to hide ID to the client. The best way is to map that DTO to Entity and do further operation. You can check more details on this link.

Ashish Karn
  • 1,127
  • 1
  • 9
  • 20
  • In such case, if the client will edit some properties of DTO, and try to save it again, it looks like id will be ignored by Spring also and the new entity will be created, isn't it? – Alexander Tsukanov Apr 27 '20 at 16:32
  • There needs to be a key that defines your entity unique and that prevents your data from the duplicate which will be sharable to the client. – Ashish Karn Apr 27 '20 at 16:52