【发布时间】:2021-06-09 04:27:05
【问题描述】:
@Entity
@Table(name="property")
@NamedQuery(name="Property.findAll", query="SELECT p FROM Property p")
public class Property implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "property_seq")
@SequenceGenerator(name = "property_seq", allocationSize = 1, sequenceName = "s_property")
@Column(name="property_id", unique=true, nullable=false, precision=10)
private long propertyId;
@Column(name="prop_size", nullable=false, precision=10, scale=4)
private BigDecimal propSize;
@Column(name="schdule_all_day_flag")
private Boolean schduleAllDayFlag;
@Column(name="schdule_prop_end_time", length=25)
private String schdulePropEndTime;
@Column(name="schdule_prop_start_time", length=25)
private String schdulePropStartTime;
@Column(name="secondary_phone", precision=10)
private BigDecimal secondaryPhone;
@Column(length=500)
private String street;
//bi-directional many-to-one association to PropertyAmenity
@OneToMany(mappedBy="property", cascade = CascadeType.ALL)
private List<PropertyAmenity> propertyAmenities;
}
@Entity
@Table(name="property_amenities")
@NamedQuery(name="PropertyAmenity.findAll", query="SELECT p FROM PropertyAmenity p")
public class PropertyAmenity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "property_amenity")
@SequenceGenerator(name = "property_amenity", allocationSize = 1, sequenceName = "s_property_amenities")
@Column(name="property_amenities_id", unique=true, nullable=false, precision=10)
private long propertyAmenitiesId;
@Column(name="avilable_flag", nullable=false)
private Boolean avilableFlag;
@Column(name="last_updated_by", length=50)
private String lastUpdatedBy;
@Column(name="last_updated_date")
private Timestamp lastUpdatedDate;
@Column(name="master_amenity_type", nullable=false, length=10)
private String masterAmenityType;
//bi-directional many-to-one association to Property
@ManyToOne
@JoinColumn(name="property_id")
private Property property;
}
在我们的项目中需要将物业和物业设施实体存储到数据库中。这 表之间的关系是property_id 作为Property 中的主键和Property Facilities 表中的FK。当使用 propertySearchRepository.save(property) 保存数据时,Property 表中的 property_id PK 不会作为 FK 级联到 Property Facilities 表并获得空值。
2021-06-09 00:20:33.347 [http-nio-9000-exec-2] 错误 o.h.e.jdbc.spi.SqlExceptionHelper - 错误:“property_id”列中的空值违反非空约束
我正在使用 spring.jpa.hibernate.ddl-auto=none,我们不应该在生产中使用 ddl 命令。请告知,如何将主键级联到子表。
【问题讨论】: