This is my entity:
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype", discriminatorType = DiscriminatorType.STRING, columnDefinition = "varchar(31) default 'RAW_QUOTATION'")
public class Quotations {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
}
I want to extend all of its properties into the following:
@Getter
@Setter
public class SingleQuotationDto extends Quotations {
private String receiverName;
}
In my service I have an object that has the type Quotations
:
Optional<Quotations> quotation = quotationRepository.findById(id);
quotation.ifPresent(quotations -> {
...
}
I want to transform the type of this object into SingleQuotationDto
, so that I can set receiverName
to it.
I know that I can do:
SingleQuotationDto singleQuotationDto = new SingleQuotationDto(quotations);
singleQuotationDto.setReceiverName("Receiver's Name");
singleQuotationDto.setDescription("description");
but quotations
is already predefined. So I don’t want to add each property manually.
Also, I consider it as a bad practice if I use setters for the entity. Since when I add new properties, I have to add their setters in this service as well. I am pretty sure there must be a cleaner solution.