【问题标题】:Hibernate JPA One To Many Primary key is not cascading as foreign key in another tableHibernate JPA 一对多主键没有级联为另一个表中的外键
【发布时间】: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 命令。请告知,如何将主键级联到子表。

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    我向您推荐的第一件事是为您的收藏设置一个默认值。在您的情况下,您可以使用空的ArrayList 初始化propertyAmenities

    @OneToMany(mappedBy="property", cascade = CascadeType.ALL)
    private List<PropertyAmenity> propertyAmenities = new ArrayList<>();
    

    然后在保存实体之前,您需要像这样设置关系的双方

    Property property = new Property();
    PropertyAmenity propertyAmenity = new PropertyAmenity();
    
    property.getPropertyAmenities().add(propertyAmenity);
    propertyAmenity.setProperty(property);
    
    propertySearchRepository.save(property);
    

    【讨论】:

    • 非常感谢。现在我可以在 propertyAmenity 下设置属性对象后保存数据 propertyAmenity.setProperty(property);
    • 不客气,请考虑accepting the answer,如果它是您问题的正确解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多