我正在将一个集合与一个集合进行映射。大多数情况下,它已成功映射,但有时却失败了,并且我得到了空值。
ArrayList<RiskRequestDTO> riskList = new ArrayList<>();
Iterable<Risk> risks = RiskRepository.findAll();
if (risks != null) {
java.lang.reflect.Type targetListType = new TypeToken<List<riskRequestDTO>>() {
}.getType();
riskList = modelMapper.map(risks, targetListType);
}
风险DTO
我在添加完整代码时遇到了一些问题,因此我删除了setter和getter。我确认在setter和getter中都存在相关注释
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"riskId", "localeTranslations", "lastModifiedAt", "lastModifiedBy"
})
public class RiskRequestDTO {
@JsonProperty("riskId")
private int riskId;
@JsonProperty("localeTranslations")
private Set<LocaleTranslation> localeTranslations;
@JsonProperty("lastModifiedAt")
private Date lastModifiedAt;
@JsonProperty("lastModifiedBy")
private UserViewDTO lastModifiedBy;
风险模型
我在添加完整代码时遇到了一些问题,因此我删除了这些实体的设置器和获取器
@Entity
@Table(name = "risk")
public class Risk {
/**
* risk_id , primary key.
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “risk_id")
private int riskId;
/**
* last modified date.
*/
@Column(name = "last_modified_at")
private Date lastModifiedAt;
/**
* details of locale.
*/
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name="risk_to_local_translation", joinColumns={ @JoinColumn(name="risk_id") }, inverseJoinColumns={ @JoinColumn(name="locale_translation_id") })
private Set<LocaleTranslation> localeTranslations;
/**
* details of the user who updated the recommendations.
*/
@ManyToOne( fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "last_modified_by", nullable = false)
private User lastModifiedBy;
我已经更新了有关DTO和实体模型的问题