I have two tables that have a parent-child relationship.
The MANUFACTURER table is the parent and the ENGINE_FAMILY table is the child.
When persisting an EngineFamily object I want the corresponding chached Manufacturer object to reflect that an EngineFamly object was added to its' set of engineFamilies. In other words, I would like the cached parent object's set of children to reflect that a new child was just added to the db.
My understanding is that if I add the attribute 'cascade="save-update" to the child's many-to-one association to its' parent, the cached parent object will immediately reflect this addition in its' set of children.
This is not happening for me. When I save the child object to the db the cached parent object does not reflect the addition of this new child. I must do a Session.refresh(parentObj) to have the parent object reflect the change.
Is my understanding of how things should work correct? I've included the relavent portion of the child's (ENGINE_FAMILY) hbm.xml file.
Hibernate version: 2.1
Level 1 cache only
Relavent Code between openSession() and session.close():
Code:
private void hibernateTest(Session hSession) {
try {
// start a Transaction
hSession.setFlushMode(FlushMode.COMMIT);
Transaction transaction = hSession.beginTransaction();
// get the mfr record
Mfr mfr = (Mfr) hSession.get(Mfr.class, new Long(10134));
// create an engine family record and save it
EngineFamily engineFamily = new EngineFamily();
engineFamily.setMfr(mfr);
engineFamily.setEngineFamilyNm("EngFamName");
engineFamily.setFeesPaidYn("N");
hSession.saveOrUpdate(engineFamily);
transaction.commit();
// At this point mfr.getEngineFamilies() does not reflect that a new
// engineFamily the was added to the database. Shouldn't it?
} catch (Exception e) {
System.out.println("Exception occurred in hibernateTest : " + e.getMessage());
}
}
Relavent part of the EngineFamily.hbm.xml file Code:
<!-- bi-directional many-to-one association to Mfr -->
<many-to-one
name="mfr"
cascade="save-update"
class="gov.epa.otaq.verify.hibernate.Mfr"
not-null="true"
>
<meta attribute="field-description">
@hibernate.many-to-one
not-null="true"
@hibernate.column name="MFR_ID"
</meta>
<column name="MFR_ID" />
</many-to-one>