-->
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: Deleting a child in a one-to-one relationship
PostPosted: Wed Dec 19, 2007 3:48 pm 
Newbie

Joined: Tue Sep 11, 2007 12:17 pm
Posts: 9
I have two entities that have a one-to-one relationship as follows:

Code:
public class ParentTable
{

    private long id;
    ...
    @OneToOne(mappedBy="Parent",cascade=CascadeType.ALL)
   @JoinColumn(name="id")
   public ChildTable getChildTable()
   {
      return childTable;
   }

   public void setChildTable(ChildTable childTable)
   {
      this.childTable=childTable;
   }
    }


public class ChildTable
{
...
   @OneToOne(targetEntity =ParetTable.class)
   @JoinColumn(name = "id", nullable = false, insertable=false, updatable=false)
public ParentTable getParentTable() {
      return parentTable;
   }

   public void setParentTable(ParentTable parentTable) {
      this.parentTable = parentTable;
   }
}



The problem is that in some cases, I want to remove the ChildTable and the ChildTable only.

So this is how I try to do it:
Code:
ParentTable parentTable=(parentTable)getHibernateTemplate().get(ParentTable.class, 12345);

ChildTable  childTable=parentTable.getChildTable();
getHibernateTemplate().delete(childTable);


However, the error I get is:
.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)

How do I remove a one-to-one association from an association. If I call childTable.setParentTable(null), it doesn't do any good.

Any idea?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 4:49 pm 
Newbie

Joined: Tue Dec 18, 2007 1:40 pm
Posts: 6
You have cascade on the inverse side of the relationship set. Since Hibernate does not manage relationships for you I would assume you need to call parentTable.setChildTable(null) before your delete.


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.