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 children
PostPosted: Wed Jan 20, 2010 3:39 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
I have an one-to-one association implemented as many-to-one with an unique constraint on the foreign key. The cascade property is "all". I expect that the children been deleted when the parent is set to null. But the children isn't deleted. How can I to do?

Mapping:

Code:
<class name="Student">
  <id name="id">
    <generator class="native"/>
  </id>
  <property name="name" not-null="true"/>
  <many-to-one name="attach" column="id_attach" unique="true" cascade="all"/>   
</class>

<class name="Attach">
  <id name="id">
    <generator class="native"/>
  </id>   
</class>


Code:

Code:
student.setAttach(null);
session.saveOrUpdate(obj);


The column id_attach is set to null in database, but the record isn't dropped from Attach table.

I wasted the day trying figure out why the record is not deleted!


Top
 Profile  
 
 Post subject: Re: Delete children
PostPosted: Thu Jan 21, 2010 9:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, nullifying the association only implies that the association is deleted, right? Why not actually delete the entity? Give it a try!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Delete children
PostPosted: Thu Jan 21, 2010 1:55 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
Suppose a CRUD application in JSF. The user edits the Student and clicks on "Delete attach" button. Then, the action do: student.setAttach(null). After, the user clicks on the "Save record" button. Now, how can I know what attach was referenced? I will need keep the reference.

The Attach is a weak entity. So, I would not like to implement a Attach Dao to delete the entity manually. See the code that I need to implement for every entity that has an association that can be deleted:

Code:
public void deleteAttach() {   
    Attach attachToBeDeleted = getPersistentObject().getAttach();
    getPersistentObject().setAttach(null);   
}

public String saveRecord() {
   ...
   if (attachToBeDeleted != null) {
       attachDao.makeTransient(attachToBeDeleted );   
   }
}


Is suck write this kind of code everywhere.

Why Hibernate can't delete the attach entity if the association was deleted?

Thank you!


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.