I’ve got two classes, ProductConfiguration and SubProduct.
I want to replace the subproducts on the configuration, which I do in this way:
productConfiguration.getSubProducts().clear();
productConfiguration.getSubProducts().addAll(newSubProducts);
In doing this, Hibernate attempts to set the ID of the parent (the product configuration) to null, and then update the row in the database. This fails, as the parent ID is a foreign key, and therefore not nullable.
The mapping from ProductConfiguration to SubProduct:
<bag name="subProducts"
table="sub_product"
cascade="all-delete-orphan"
access="field"
lazy="false">
<key column="parent_id"/>
<one-to-many class="com.conscius.cpt.core.product.SubProduct"/>
</bag>
<many-to-one name="parentProduct"
class="com.conscius.cpt.core.product.ProductConfiguration"
column="parent_id"
access="field"
not-null="true"
cascade="none"/>
The solution here is simple. I forgot to add an inverse statement to the product configuration side.