I want to add an additional property to my entity (Quotations
) and tried that with this DTO:
@Getter
@Setter
public class SingleQuotationDto {
@Delegate
private Quotations quotations;
private String receiverName;
}
This method should do the job:
public Optional<SingleQuotationDto> getSingleQuotation(String token, Long id) {
String username = getUsername(token);
SingleQuotationDto singleQuotationDto = new SingleQuotationDto();
Optional<Quotations> quotation = quotationRepository.findById(id);
quotation.ifPresent(quotations -> {
List<PendingQuotations> pendingQuotations = pendingQuotationRepository.findAllByQuotationId(id);
for (PendingQuotations pendingQuotation : pendingQuotations) {
if (pendingQuotation.getReceiver().equals(username)) {
User user = userRepository.findByUsername(username);
String name = userProfileRepository.findByUser(user).getName();
singleQuotationDto.setReceiverName(name);
break;
}
}
singleQuotationDto.setQuotations(quotations);
});
return Optional.of(singleQuotationDto);
}
The problem is its output:
{
"quotations": {
"id": 284,
"description": "Description"
},
"id": 284,
"description": "Description",
"receiverName": "Name",
}
As you see, the properties from Quotations
are duplicated. This is what I expect:
{
"id": 284,
"description": "Description",
"receiverName": "Name",
}
What am I doing wrong?