Я хочу сопоставить несколько полей класса Dto с одним полем сущности и наоборот. Как этого добиться?JAVA

Программисты JAVA общаются здесь
Гость
Я хочу сопоставить несколько полей класса Dto с одним полем сущности и наоборот. Как этого добиться?

Сообщение Гость »


Итак,
у нас есть класс Entity — Contact

Код: Выделить всё

public class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name",nullable = false)
private String name;
@Column(name = "email",nullable = false,unique = true)
private String email;
@Column(name = "phone",nullable = false)
private String phone;
}
A DTO class - ContactDto

Код: Выделить всё

public class ContactDto {
private Long id;
private String fname;
private String lname;
private String email;
private String phone;
}
I want to map the fname and lname with the name attribute in the contact and vice versa. How to achieve this solution?
I tried using MapStruct library. I am getting confused how to implement this logic. So kindly help me either with MapStruct or ModelMapper.

Код: Выделить всё

@Mapper
public interface ContactMapper {
ContactMapper INSTANCE = Mappers.getMapper(ContactMapper.class);
ContactDto mapToContactDto(Contact contact);
Contact mapToContact(ContactDto contactDto);
}
This is what i tried doing using Mapstruct.


Источник: https://stackoverflow.com/questions/781 ... entity-and

Вернуться в «JAVA»