[英]Mapstruct many-to-one mapping
我對 mapstruct 中的 @ManyToOne 映射有疑問。 我有兩張桌子
第一個:
@Entity
@Table(name = "members", schema = vsm)
public class MemberEntity{
@Column(name = "id", nullable = false)
protected Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "case_id", nullable = false)
private CaseEntity case;
}
第二個:
@Entity
@Table(name = "cases", schema = vsm)
public class CaseEntity {
@Column(name = "id", nullable = false)
protected Long id;
@Column(name = "description", nullable = false)
protected String description;
}
我有一個像這樣的案例:
public class CasesDto{
protected Long id;
protected String description;
private List<MemberDto> members;
}
和 MemberDto 同為實體。
我需要像這樣使用 mapstruct 進行映射:
CasesDto mapToDto(CaseEntity entity);
我需要填寫 List 成員; 但我無法理解如何。
您不能簡單地以這種方式將其映射到 CasesDto。 沒有要映射到List<MemberDto>
集合。 您應該擁有完全組合的 CaseEntity ,其中包括:
@OneToMany(mappedBy = "cases")
private List<MemberEntity> members = new ArrayList<>();
為了避免最壞的 N+1 問題( stackOverflow N+1 ),您應該正確配置 JPA(簡短的 spring .yml 示例),這將允許您執行1 + Math.ceil({MembersAmount}/{default_batch_fetch_size})
查詢ORM:
spring:
jpa:
properties:
hibernate:
jdbc:
batch_size: 50
default_batch_fetch_size: 50
或者使用您的附加查詢獲取成員列表,並以這種方式將其合並到 Mapstruct 中(我認為這是一種更糟糕的選擇):
@Mapping(target = "members", source = "members")
CasesDto toDto(CaseEntity entity, List<MemberEntity> members);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.