I\'m refactoring my code. I want to use java records instead of java class in my DTO. To convert DTO to Entity, I\'m using ModelMapper (version 2.3.5). When I try to get inf
record
is a preview feature in Java 14 so I would recommend you to not use it on production. Secondly, it doesn't mimic java bean.
record
doesn't have a default no arg constructor implicitly if there are fields. If you wrote a no arg constructor, you will have to delegate the call to all args constructor and since all the fields are final
you can only set them once. So you are kind of stuck there. See JEP 359:
It is not a goal to declare "war on boilerplate"; in particular, it is not a goal to address the problems of mutable classes using the JavaBean naming conventions.
An alternative that works today would be to use Lombok. Example of UserDto
using Lombok:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserDto {
private String firstName;
private String lastName;
private String email;
private String imageUrl;
}