(Java) How should I implement code for 2 semantically identical entities that need to be stored in different DB tables?

We are an E-commerce company and so far, we have been using an entity called DeliveryAddressEntity to represent address data stored when we receive orders. I am trying to add support for BillingAddressEntity, which would have the exact same fields as DeliveryAddress, but I need it to be stored in a different table. So far I have included an abstract class, called AddressEntity:

@Getter
@Setter
@MappedSuperclass
@FieldNameConstants
public class AddressEntity extends AbstractEntity<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;
    
    @Column(name = "order_id")
    private Long orderId;
    
    @Column(name = "address_line_1", length = 500)
    private String addressLine1;
    
    @Column(name = "address_line_2", length = 500)
    private String addressLine2;

}

etc…

from which DeliveryAddress and BillingAddress inherit from:


    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Entity
    @Table(name = "billing_address")
    @Audited(withModifiedFlag = true)
    @AuditOverride(forClass = AbstractEntity.class)
    @FieldNameConstants
    public class BillingAddressEntity extends AddressEntity {

}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "delivery_address")
@Audited(withModifiedFlag = true)
@AuditOverride(forClass = AbstractEntity.class)
@FieldNameConstants
public class DeliveryAddressEntity extends AddressEntity {

}

The problem I’m facing is that a lot of the relevant business logic is written for the DeliveryAddressEntity class, and refactoring it is proving to be tough. For example there is a method:


@Override
public DeliveryAddressEntity toDeliveryAddressEntity(OrderMessage orderMessage) {
DeliveryAddressEntity deliveryAddress = orderMessageMapper.toAddressEntity(
orderMessage.getDeliveryAddress());
return deliveryAddress;
}

I would like to simply reuse this method for BillingAddressEntity, however this is not possible due to it returning the wrong type. I have tried different ways of refactoring like using the AddressMessage class as both abstract and non abstract. However, I can’t help feeling there is a simpler solution. The two types of Addresses are literally identical; they just need to saved to different DB tables. Perhaps I need a middle-man class To perform all the business logic on, and only at the point of saving to the DB I need to convert to the corresponding entity?
Thanks

Tried to refactor methods a lot, but seems hard keeping in mind I want to avoid repeated code, and also altering Objects passed to methods as a parameter. For example I tried to extract the setter methods from


public DeliveryAddressEntity toDeliveryAddressEntity(DeliveryAddressMessage request) {
if (request == null) {
return null;
}
DeliveryAddressEntity deliveryAddress = new DeliveryAddressEntity();
deliveryAddress.setAddressLine1(request.getAddressLine1());
deliveryAddress.setAddressLine2(request.getAddressLine2());
deliveryAddress.setCity(request.getCity());
deliveryAddress.setState(request.getState());
deliveryAddress.setCountryCode(request.getCountryCode());
deliveryAddress.setHouseNumberOrName(request.getHouseNumberOrName());
deliveryAddress.setPostcode(request.getPostcode());
return deliveryAddress;
}

public BillingAddressEntity toBillingAddressEntity(DeliveryAddressMessage request) {
if (request == null) {
return null;
}
BillingAddressEntity billingAddress = new BillingAddressEntity();
billingAddress.setAddressLine1(request.getAddressLine1());
billingAddress.setAddressLine2(request.getAddressLine2());
billingAddress.setCity(request.getCity());
billingAddress.setState(request.getState());
billingAddress.setCountryCode(request.getCountryCode());
billingAddress.setHouseNumberOrName(request.getHouseNumberOrName());
billingAddress.setPostcode(request.getPostcode());
return billingAddress;
}`your text`

New contributor

yorkiepudpud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật