Save Entity with relationship JPA code architecture

I am building a RESTful API in Spring boot. My architecture is the three-tier achitecture; Controller -> Service -> Repository.

I have 2 entities, Transaction and Invoice. This is a OneToOne relationship. A Transaction may or may not have an Invoice, but an Invoice must have a Transaction.
I am also using the DTO pattern which has the services return DTO objects of the Entity back to the calling Controller which then gets returned back to my frontend.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "invoice")
public class Invoice {
/**
Omitted fields
**/
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JsonManagedReference(value = "transaction-invoice")
private Transaction transaction;
}
</code>
<code>@Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Table(name = "invoice") public class Invoice { /** Omitted fields **/ @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) @JsonManagedReference(value = "transaction-invoice") private Transaction transaction; } </code>
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "invoice")
public class Invoice {

    /**
       Omitted fields
    **/


    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JsonManagedReference(value = "transaction-invoice")
    private Transaction transaction;

}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "transaction")
public class Transaction {
/**
Omitted fields
**/
@OneToOne
@JsonBackReference(value = "transaction-invoice")
private Invoice invoice;
}
</code>
<code>@Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Table(name = "transaction") public class Transaction { /** Omitted fields **/ @OneToOne @JsonBackReference(value = "transaction-invoice") private Invoice invoice; } </code>
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "transaction")
public class Transaction {

    /**
       Omitted fields
    **/

    @OneToOne
    @JsonBackReference(value = "transaction-invoice")
    private Invoice invoice;

}

I have a POST endpoint in my InvoiceController which calls an InvoiceService. The InvoiceService builds and saves the created Invoice Entity, using an InvoiceRepository (JpaRepository).

However, when this is being created I also need to save the link to an existing Transaction entity. I am using JpaRepositories and not entitymanager.

Options:

  1. Should I have TransactionRepository in my InvoiceService directly and get a proxy entity for Transaction and save the link, which then uses cascading to to update the Transaction table?
  2. Should I have TransactionService in my InvoiceService, which calls the getTransaction method – which in turn calls the TransactionRepository to get a proxy entity. Problem here, is since I am using the DTO pattern, my services return DTO objects of the Entity, which I would need to map into the Entity before using in my InvoiceService, creating a lot of overhead.

Are there any other options to do this?

I feel like both of these options infringe the Separation of Concerns.

This is not the only time this scenario comes up, so I am wondering the best way to deal with this.

TLDR;
The issue I’m facing though is whether the correct way to lay the code out to still adhere to separation of concerns, in my three-tier architecture, when creating an Invoice Entity but also saving the link to an existing Transaction Entity.

12

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