我正在尝试在子级(ZonePoint_DTO)和父级(ZoneDTO)之间建立双向关系,其中父级具有复合键。
当我尝试输入带有子实体(ZonePoint_DTO)实体列表的整个父对象(ZoneDTO)时,当前映射导致在子表(ZonePoint_DTO)中输入空值。请注意,我已将层叠类型设置为Cascade.PERSIST。
我正在使用JpaRepository.save()保存Parent(ZoneDTO)
下面是实体类。
@Entity
@Getter
@Setter
public class ZoneDTO implements GenericPJM<ZoneDTO_Id>, Persistable<ZoneDTO_Id> {
private static Set<String> zoneNamesList;
@EmbeddedId private ZoneDTO_Id id;
private Integer noOfPolygons;
@OneToMany(mappedBy = "zoneDTO", fetch = FetchType.EAGER)
@Cascade(value = org.hibernate.annotations.CascadeType.PERSIST)
private List<ZonePointDTO> zonePointsList;
@Override
public boolean isNew() {
boolean result = !zoneNamesList.contains(id.getZoneName());
System.out.println(
"__________________________isNew() called for " + this.getClass().getSimpleName() + "result= " + result);
return result;
}
public static void setZoneNamesList(Set<String> zoneNamesListNew) {
zoneNamesList = zoneNamesListNew;
}
}
@Getter
@Setter
@Embeddable
public class ZoneDTO_Id implements GenericPJM_Id {
String zoneName;
}
@Entity
@Getter
@Setter
public class ZonePointDTO implements GenericPJM<Long> {
@JsonIgnore @Id @GeneratedValue private Long id;
private double xValue;
private double yValue;
private Integer polygonNumber;
@ManyToOne private ZoneDTO zoneDTO;
}
我发送的JSON格式:
{
"type":"ZoneDTO",
"id": {"zoneName":"SAMPLE"},
"noOfPolygons": 1,
"zonePointsList": [
{
"type":"ZonePointDTO",
"xValue": 10,
"yValue": 39.5,
"polygonNumber": 1
},
{
"type":"ZonePointDTO",
"xValue": 10,
"yValue": 39,
"polygonNumber": 1
}
]
}
我想一次级联持久保存父对象(ZoneDTO),并希望最小化数据库调用。 当前,父母和孩子的数据都在保留。但是子映射列将保存空值。
任何评论或解决方法或其他方法都将有所帮助。 预先感谢。