【发布时间】:2022-01-10 12:59:52
【问题描述】:
我有这样的物业实体和平面图实体
@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”列中的空值违反了非空约束
【问题讨论】:
-
首先要么删除父级,要么将字段 property_id 更新为新平面图的值。这个错误只是说你的数据库有一个约束,其中必须存在一个关系 floor_plans。
-
我没有新的平面图只是想删除它们并保存
-
由于数据库的限制,这是不可能的