我有這樣的物業物體和平面圖物體
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "properties", indexes = {@Index(name = "properties_parent_id_index", columnList = "parent_id", unique = true)})
public class PropertyEntity extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "property_id")
private List<FloorPlanEntity> floorPlans;
....
...
}
現在我嘗試洗掉孩子并保存父母
像這樣`
propertyEntity.getFloorPlans().clear();
propertyDataService.save(propertyEntity);
但它給出了這樣的例外
引起:org.postgresql.util.PSQLException:錯誤:關系“floor_plans”的“property_id”列中的空值違反了非空約束
uj5u.com熱心網友回復:
我認為您可以使用@OneToMany(orphanRemoval = true)
它提供了一種從資料庫中洗掉孤立物體的方法。因此,當它從串列中洗掉時,它應該從資料庫中洗掉 FloorPlanEntity。有一些帶有解釋的教程:https :
//www.baeldung.com/jpa-cascade-remove-vs-orphanremoval#orphanremoval
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/408629.html
標籤:
