I have seen this issue discussed several times before but I still cannot find a definitive answer...
Basically, we have a unidirectional one-to-many mapping like so:
Code:
public class DraftProfile {
private Collection<DraftPlan> draftPlans;
....
}
Code:
<hibernate-mapping>
<class name="domain.DraftProfile">
<bag name="draftPlans" cascade="all,delete-orphan" access="field">
<key column="profile_id" not-null="true"/>
<one-to-many class="domain.DraftPlan"/>
</bag>
</hibernate-mapping>
We add a DraftPlan to a DraftProfile like so:
Code:
// open session and retrieve Profile
profile.addDraftPlan(draftPlan);
save(profile);
// commit
The comment represent functionally what is going on - the transaction and Session management is handled by Spring. Pretty sure this is a mapping issue, so this does not seem to matter.
Anyway, when this is executed, we see two statements, an insert and an update:
Code:
Hibernate:
insert
into
draft_plan
(draft_type, loan_number, enrollment_channel, status, service_fee, payment_selection_type, payment_amount, late_
charge, principal, escrow, fees_due, credit_life, interest, other_amount, profile_id, start_date, plan_type)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'ONE_TIME ')
Hibernate:
update
draft_plan
set
profile_id=?
where
plan_id=?
I have an idea of the "why" behind this, as it was documented well at
http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html#example-parentchild-bidir and
http://forums.hibernate.org/viewtopic.php?t=959465.
However, what I still have not found an answer for is can this be solved
without having a bidirectional relationship? All solutions I have read simply say add inverse="true", but does this property even have any meaning for unidirectional relationshipts.
What I want is to have a unidirectional relationship in my object model and for Hibernate
not to issue an insert after an update when I add to the child collection. Is this possible?