-->
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 load() children when loading parent
PostPosted: Tue Aug 02, 2005 12:16 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">

   <class
      name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
      table="CCC.AGNT_MSG">

      <id name="id" type="long">
         <column name="MSG_ID" sql-type="NUMBER" />
            <generator class="sequence">
            <param name="sequence">CCC.SEQ_AGNT_MSG</param>
         </generator>
      </id>

      <set name="recipients" cascade="all" lazy="false">
         <key column="MSG_ID" />
         <one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
      </set>

      <property name="fromDate" type="date">
         <column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="toDate" type="date">
         <column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
      </property>
      
      <property name="messageText" type="string">
         <column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
      </property>

      <property name="createUser" type="string">
         <column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="createDate" type="date">
         <column name="CREATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="modUser" type="string">
         <column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="modDate" type="date">
         <column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="categoryCode" type="string">
         <column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <property name="behaviorCode" type="string">
         <column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <many-to-one name="messageCategoryDVO" column="MSG_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
      <many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>   

   </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">

   <class
      name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
      table="CCC.MSG_RECIPIENT">

      <composite-id>
         <key-many-to-one name="myParent" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO">
            <column name="MSG_ID"/>
         </key-many-to-one>
         <key-property name="recipientId" type="string" column="RECIPIENT_ID" />
      </composite-id>

      <property name="receiverCategoryCode" type="string">
         <column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true" />
      </property>

      <many-to-one name="myParent"   column="MSG_ID"        class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO" not-null="true"     insert="false" update="false" />
      <many-to-one name="recvCatgCd" column="RCVR_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>

   </class>

</hibernate-mapping>


Code for duplicating record/object:
Code:
   public void duplicate(Long messageId, String createUser) throws InfrastructureException {
      logger.debug("duplicate: start: messageId: " + messageId);
      logger.debug("duplicate: get session");
      Session session = HibernateUtil.getSession();
      logger.debug("duplicate: initial blank AgentMessageDVO");
      AgentMessageDVO agentMessageDVO = null;
      try {
         logger.debug("duplicate: get AgentMessageDVO by id: " + messageId);
         agentMessageDVO = (AgentMessageDVO) session.load(AgentMessageDVO.class, messageId);
         logger.debug("duplicate: children: type: " + agentMessageDVO.getChildren().getClass() );
         Vector xyz = (Vector)agentMessageDVO.getChildren();
         logger.debug("duplicate: children: size: " + xyz.size() );
         
         AgentMessageDVO newAgentMessageDVO = new AgentMessageDVO();
         newAgentMessageDVO.setChildren( agentMessageDVO.getChildren() );
         newAgentMessageDVO.setFromDate( agentMessageDVO.getFromDate() );
         newAgentMessageDVO.setToDate( agentMessageDVO.getToDate() );
         newAgentMessageDVO.setBehaviorCode( agentMessageDVO.getBehaviorCode() );
         newAgentMessageDVO.setCategoryCode( agentMessageDVO.getCategoryCode() );
         newAgentMessageDVO.setCreateUser( createUser );
         newAgentMessageDVO.setCreateDate(new Date());
         newAgentMessageDVO.setModUser( createUser );
         newAgentMessageDVO.setModDate(new Date());
         newAgentMessageDVO.setMessageText( agentMessageDVO.getMessageText() );
         
         
         logger.debug("duplicate: call session update");
         HibernateUtil.getSession().saveOrUpdate(newAgentMessageDVO);
      }  catch (HibernateException ex) {
         logger.debug("duplicate: HibernateException: " + ex.getMessage() );
         throw new InfrastructureException(ex);
      }  catch (Exception e) {
         logger.debug("duplicate: Exception: " + e.getMessage() );
         throw new InfrastructureException(e);
      }      
      logger.debug("duplicate: finish");
   }


Name and version of the database you are using: Oracle 9i

Ok, so I have a parent/child mapping. I can create the parent object, create the children, add them to the parent ... and when I save, I can save the parent and the children in one save. No problem there.

I want to be able to duplicate this object in the database. So, given an ID, I create a session and do a load(). The Parent object gets loaded, BUT the children DO NOT?!?!?!

I would presume that because of the mapping, when I load a parent, the children would come with it.

As it stands, I can duplicate the parent object just fine by creating a new instance of the parent, and then copying over the needed fields. So, if I got the children from the original parent, then I would be able to copy over that children to the new parent.

So, what is wrong with my mapping that only the parent is load()ing, but not the children.

Thanks for any help!


Top
 Profile  
 
 Post subject: SOLVED problem with load()
PostPosted: Wed Aug 03, 2005 9:51 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
My DVO class which is a POJO is extended from another class called BaseDVO. I didn't realize it at the time, but it had a "getChildren" method.

I thought that when I load()ed an object from Hibernate, it may have retrieved some other type of object with a getChildren method ... but it didn't.

So, what I should have done was call my "getRecipients" methods which is defined as holding my collection. Howver, these children WERE a new class which I had never seen before. An org.hibernate.Collection.PersistentSet

So, I used the iterator method from that, and I got all my children out ... just like I should have.

That only solved on problem ... now I have to go investigate other issues.


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.