-->
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.  [ 5 posts ] 
Author Message
 Post subject: Am I working this association correctly?
PostPosted: Wed Dec 06, 2006 11:22 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Could someone please clarify something for me?

This code doesn't error, but it doesn't save the 'call' object which is a child of 'checklist'. I would have hoped that when I call 'checklist.getSecurityCalls().put(...)' Hibernate would be smart enough to save (thereby generating the new call PK under the hood) or updating the existing object.

Code:
      ChecklistValue checklist = (ChecklistValue) getHibernateTemplate().get(ChecklistValue.class, new Integer(checklistId));
      Iterator keys = securityCalls.keySet().iterator();
      while(keys.hasNext()) {
         Integer key = (Integer)keys.next();
         SecurityCallValue call = (SecurityCallValue)securityCalls.get(key);
         updateLastModifiedOn(call);         
         checklist.getSecurityCalls().put(key, call);
      }   
      getHibernateTemplate().update(checklist);



This code does save the child object. But, only because I'm saving it explicitly.

Code:
      ChecklistValue checklist = (ChecklistValue) sess.get(ChecklistValue.class, new Integer(checklistId));
      Iterator keys = securityCalls.keySet().iterator();
      while(keys.hasNext()) {
         Integer key = (Integer)keys.next();
         SecurityCallValue call = (SecurityCallValue)securityCalls.get(key);
         updateLastModifiedOn(call);         
         Integer id = (Integer)sess.save(call);
         call.setSecurityCallId(id.intValue());
         checklist.getSecurityCalls().put(key, call);
      }


I'm having a little trouble understanding how Hibernate is being useful to me in regards to the one-to-many relationship I have. It's not saved me any lines of code. I can understand that the relationship saves me some time when reading the checklist back in (complete with my map of 'call' objects though.

Am I missing something in the first listing that would make it work in the way I expect?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 11:28 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Could you post your mapping files for CheckList and SecurityCall? It might be a cascade issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 11:33 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Sure, here you go:

ChecklistValue
Code:
   
    <class name="com.lsb.uk.mqs.value.ChecklistValue" table="CHECKLIST">
      <id name="checklistId" column="CHECKLIST_ID" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">checklist_seq</param>
         </generator>
      </id>         
      <property name="applicationId" column="APPLICATION_ID"/>
      <property name="adHocRequirements" column="AD_HOC_REQUIREMENTS"/>
      <property name="applicationException" column="APPLICATION_EXCEPTION"/>      
        <property name="lastUpdateUser" column="LAST_MODIFIED_BY"/>
        <property name="lastUpdateDate" column="LAST_MODIFIED_ON"/>         
      <map name="securityCalls" inverse="true" lazy="false">
          <key column="CHECKLIST_ID"/>
          <map-key column="APPLICANT_ID" type="integer"></map-key>
          <one-to-many class="com.lsb.uk.mqs.value.SecurityCallValue"/>
      </map>
    </class>



SecurityCallValue
Code:

    <class name="com.lsb.uk.mqs.value.SecurityCallValue" table="SECURITY_CALL">
      <id name="securityCallId" column="SECURITY_CALL_ID" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">security_call_id_seq</param>
         </generator>
      </id>         
      <property name="checklistId" column="CHECKLIST_ID"/>
      <property name="passed" column="PASSED"/>      
      <property name="callTime" column="CALL_TIME"/>
      <property name="applicantId" column="APPLICANT_ID"/>
      <property name="lastUpdateUser" column="LAST_MODIFIED_BY"/>
        <property name="lastUpdateDate" column="LAST_MODIFIED_ON"/>
    </class> 


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 1:59 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Hibernate does not cascade any operation by default. You need to add the cascade property to the mapping file where you declare the child <map> element, using cascade= "save-update".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 3:24 pm 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
I'll give it a try in the morning. I'm sure that'll do it.

Thanks for the tip, adding a thumbs-up rating for your help!

Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.