-->
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.  [ 4 posts ] 
Author Message
 Post subject: Delete child of a parent-child one-to-one relationship?
PostPosted: Thu Feb 14, 2008 5:40 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Hibernate version:
Hibernate 3.0
Name and version of the database:
Microsoft SQL Server 2005
Mapping documents:
Capability.hbm.xml:
Code:
<hibernate-mapping package="emst.dao.entities.capabilities">
   <class name="Capability" table="CAP_Capability">
      <id name="id" column="id" type="long">
         <generator class="native" />
      </id>
      <many-to-one name="currentAction" class="emst.dao.entities.capabilities.actions.Action" cascade="all" lazy="false"/>
   </class>
</hibernate-mapping>

Action.hbm.xml:
Code:
<hibernate-mapping package="emst.dao.entities.capabilities.actions">
   <class name="Action" table="CAP_Action">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <property name="timeForCompletion" type="integer"/>
   </class>
</hibernate-mapping>

Code:
Code:
public class Capability {
   private Long id;
   private Action currentAction;
   public Capability(){
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public Action getCurrentAction() {
      return currentAction;
   }

   public void setCurrentAction(Action currentAction) {
      this.currentAction = currentAction;
   }
}

Problem:
The relationship between Capability and Action is a variation of the parent-child relationship. Throughout the lifespan a Capability will assign new Action objects to currentAction via:
Code:
this.setCurrentAction( SOME NEW ACTION)

When this happens I'd like Hibernate to delete the previously stored Action. As of now every Action object remains in the database even after all of the Capability objects have been removed.

If I were to make currentAction a list or map then it gives the behavior I want but currentAction will only have a single value. Anybody know how to get this behavior without needlessly making currentAction a collection?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 6:19 pm 
Newbie

Joined: Thu Feb 14, 2008 4:26 pm
Posts: 3
Warning a Hibernate rookie replies...

Code:
   public Action replaceCurrentAction(Action currentAction) {

     Action oldAction = this.currentAction;
     this.currentAction = currentAction;
     return oldAction;
   }

   ...
   Action oldAction = myCapability.replaceCurrentAction(newAction);
   session.delete( oldAction );


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 8:03 am 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Its funny that you suggested that fix because that is what I'm currently using to workaround the problem and I'm somewhat of a rookie as well. I was just hoping there was a more elegant solution....if Hibernate took care of it I imagine the my code will be easier to work with for whoever gets stuck maintaining it. Thanks for the suggestion though, any help is appreciated!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 1:26 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Does anybody have a more elegant solution to this issue?


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