在保存期间,未为application / xml请求有效内容保存子实体外键。 但是在dto中使用@JsonBackReference时,可以正常使用json输入数据。
@Entity
@Table(name="REQUEST")
public class MsaDisabScreenRequest implements Serializable {
@Id
@Column(name="REQUEST_ID")
private long requestId;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy="msaDisabScreenRequest")
private Set<ReqDetail> disabilities;
}
@Entity
@Table(name="REQ_DETAILS")
public class ReqDetail implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="MAP_ID")
private long mapId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="REQUEST_ID")
privateRequest msaDisabScreenRequest;
}
这是我用来映射的dto。
import com.fasterxml.jackson.annotation.JsonManagedReference;
@XmlRootElement(name="DisabilityRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class MsaDisabScreenRequestDto implements Serializable {
private static final long serialVersionUID = 1L;
private long requestId;
@NotNull(message="{disabilities.required}")
@JsonManagedReference // when added works json payload
private Set<DetailDto> disabilities;
}
import com.fasterxml.jackson.annotation.JsonBackReference;
@XmlRootElement(name="disabilities")
@XmlAccessorType(XmlAccessType.FIELD)
public class MsaDisabScreenReqDetailDto implements Serializable {
private static final long serialVersionUID = 1L;
private long mapId;
@JsonBackReference // when added works json payload
private RequestDto msaDisabScreenRequest;
}
保存使用jpa存储库保存的代码。
// mapping
Request request = mapper.map(requestDto,Request.class);
Request Res = msaRepository.save(request);
这里由于 @JsonBackReference 使用(我猜)JSON请求有效负载,request_Id将保存在子表中。 但是对于xml有效负载,请求Id为空。
这种行为会是什么原因?是否有任何@JsonBackReference等效注释用于XMl有效负载?另一种解决方案是什么?