-->
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.  [ 3 posts ] 
Author Message
 Post subject: Delete/Replace Childrem parent gets deleted also?????
PostPosted: Thu Aug 04, 2005 12:17 am 
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 >

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

      <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-delete-orphan" lazy="false" inverse="true" >
         <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>

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

      <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" cascade="all" fetch="select" lazy="true" />
      <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 between sessionFactory.openSession() and session.close():
Code:
public void makePersistent(AgentMessageDVO agentMessageDVO) throws InfrastructureException {
      logger.debug("makePersistent: start");
      try {
         logger.debug("makePersistent: saveOrUpdate: start");
         HibernateUtil.getSession().saveOrUpdate(agentMessageDVO);
         logger.debug("makePersistent: saveOrUpdate: finish");
      } catch (HibernateException ex) {
         logger.debug("makePersistent: HibernateException: " + ex.getMessage() );
         throw new InfrastructureException(ex);
      } catch (Exception e) {
         logger.debug("makePersistent: Exception: " + e.getMessage() );
         throw new InfrastructureException(e);
      }
      logger.debug("makePersistent: finish");
   }


Full stack trace of any exception that occurs: N/A

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

The generated SQL (show_sql=true):
Code:
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.AGNT_MSG where MSG_ID=?


Debug level Hibernate log excerpt: N/A


I want to update records in my database. So, if I don't touch any children records, I can modify the parent fields with no problems.

But, because of the way my app is ... the children may change all the time. So, I presumed it would be easier to remove them all, and just add the new children.

I tried to just delete the old set and add a new set, and that didn't work. When I used parent.getChildren().clear(); and then did a session.flush. Not only did it get rid of the children, but the parent as well. No! I want to keep the parent and delete the children.

I only want to delete the children IF AND ONLY IF I delete the parent. But, if I delete all the children, then I want to keep the parent, and then add new children.

So, I tried and iterative process to delete one child at a time, and since I deleted all the children ... it removed the parent as well.

Please ... any help would be most appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 12:52 am 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think the parent gets deleted because of cascade="all"
on the relationship. Remove that and the parent shouldn't get deleted.

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


Top
 Profile  
 
 Post subject: SOLVED prob with parent deleted when deleting children
PostPosted: Thu Aug 04, 2005 11:02 am 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
This drove me nuts for hours, but the previous comment, kinda pointed me in the right direction.

In the parent hbm.xml mapping file I changed the set cascade to LOTS of different things and I got lots of different results. But ultimately I left it alone at all-delete-orphan.

HOWEVER, in the child mapping file, the reference back to the parent looked like this:
Code:
<many-to-one name="myParent"   column="MSG_ID"        class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"     not-null="true" insert="false" update="false" cascade="all" fetch="select" lazy="true" />



Well, I got rid of the stuff I didn't think I needed like the cascade, fetch, and lazy attributes. So, now it looks like this:
Code:
<many-to-one name="myParent"   column="MSG_ID"        class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"     not-null="true" insert="false" update="false" />


Now when I delete the child records, the parent sticks around.


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