I had a question about cascades. I've seen examples where a parent is created, it is given the required information, persisted to the database, and then the children are added. The cascade will nicely save the children out.
However, what if the parent is created along with all of its children *before* it is persisted to the database the first time? That's what I'm trying to do and it keeps giving me an error:
"org.hibernate.exception.ConstraintViolationException: could not insert:"
because the foreign key on the child object is null. My mapping is as follows (children of a "Product"):
Code:
<bag name="features" cascade="all-delete-orphan">
<key column="product_id"/>
<one-to-many class="com.rcwilley.business.product.Feature"/>
</bag>
<bag name="specifications" cascade="all-delete-orphan">
<key column="product_id"/>
<one-to-many class="com.rcwilley.business.product.Specification"/>
</bag>
I probably misunderstand the process but it seems like one should be able to create and populate a parent along with give it lists of subordinate data and then Hibernate should be able to sort out the order of the persistence, namely save the parent, get its ID, and use that ID to create the children objects.
So my question is, do I really have to persist the parent object *before* adding children objects to it or is there a way to have it save everything in one call to session.save(product)?