簡體   English   中英

與DTO相關的實體的實體

[英]Entity with associated entity to DTO

我想問一個將帶有關聯實體的實體轉換為DTO的良好實踐。

因此,例如,我有3個實體:

@Entity
@Table
public class First {
    @Id
    private int id;

    @OneToMany(mappedBy = "first")
    ...
    private List<Second> second;

    @ManyToOne
    ...
    private Third third;


@Entity
@Table
public class Second {
    @Id
    private int id;

    @ManyToOne
    ...
    private First first;

    @OneToMany(mappedBy = "second")
    ...
    private List<Third> third;



@Entity
@Table
public class Third {
    @Id
    private int id;

    @ManyToOne
    ...
    private Second second;

    @OneToMany(mappedBy = "third")
    ...
    private List<First> first;

將所有這些轉換為DTO的最佳實踐是什么? 我不想使用外部庫。

就像您看到的那樣,問題出在它的重復性和嵌套賦值上。

問候。

編輯:有人可以給我我自己使用的映射DTO的庫名稱嗎?

這可能會有所幫助:

我建議使用自定義dto以避免循環(導致StackOverFlowError)。 管理要傳輸的內容的優勢。 使用上面的實體類,我在下面構造了dto。

我對龍目島有依賴性
“ org.projectlombok”%“ lombok”%lombokVersion

在我的構建中。

構建了我的自定義DTO

@Data
public class FirstDTO implements Jsonable {
   protected String id; 
   protected ThirdDTO third;

   public FirstDTO(){}

   @JsonCreator
   public FirstDTO(@JsonProperty("id") String id,
                     @JsonProperty("third") Third third){
          this.id = id;
          this.third = third;
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM