-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Can't get cascade="save-update" to work correctly
PostPosted: Mon Dec 05, 2005 11:49 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
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>



Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 17, 2005 2:22 pm 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
I understand that MANUFACTURER is the parent and contains a set of ENGINE_FAMILY objects in the property 'engineFamilies'. The manufacturer is referenced via a foreign key from the ENGINE_FAMILY table.

So in the MANUFACTURER you define the following property:

Code:
...
<set name="engineFamilies"
     cascade="all,delete-orphan"
     inverse="true">
   <key column="MANUFACTURER_ID"/>
   <one-to-many class="EngineFamily"/>
</set>
...


In the ENGINE_FAMILY you may have the mapping the the parent (the manufacturer):

Code:
<many-to-one name="manufacturer" column="MANUFACTURER_ID" class="Manufacturer" unique="true" not-null="true"/>


Now you can add and remove engines from the EngineFamily of the manufacturer and that change is persisted. If you remove an engine from the set, the engine is removed from the set as well as deleted from the engine family's data table.

Does this help?

All the best,

René


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.