我正在尝试在数据库中插入一个对象。它包含一个也应该插入的集合,但我得到以下异常:
SQLServerException: Cannot insert the value NULL into column 'ID_CUSTOMER', table 'dbo.CUSTOMER_ADDRESS'; column does not allow nulls. INSERT fails.
映射如下:
客户数据库.java
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer", orphanRemoval = true, cascade = CascadeType.ALL)
private List<CustomerAddressDB> addresses;
CustomerAddressDB.java
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "ID_CUSTOMER", nullable = false, updatable=false, insertable=false)
private CustomerDB customer;
因此,当我为 CustomerDB 实体调用 persist 方法时,我可以看到 HQL 正在执行:
Hibernate: insert into M2M_CUSTOMER (NM_COMPANY_NAME, DT_CREATED, ID_PARENT, ID_LAYER, DS_OBS, DS_SHORT_NAME, ID_CUSTOMER_STATUS, NM_TRADING_NAME) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into M2M_CUSTOMER_ADDRESS (DS_CITY, DS_COUNTRY, NR_NUMBER, NR_POSTAL_CODE, DS_STATE, DS_STREET, ID_ADDRESS_TYPE) values (?, ?, ?, ?, ?, ?, ?)
但在此之后,前面提到的异常被抛出。我不明白为什么它说 CUSTOMER_ID 为空,因为客户被插入到地址之前。有没有映射错误?
谢谢!