-->
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: automatically delete child object when it is null
PostPosted: Tue Sep 18, 2007 3:42 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I have a strange problem - I can't find a solution however I guess it
is rather common.

I have two classes: Person and RegistrationAddress. A Person has a
RegistrationAddress:

<class name="org.dyndns.orzekanie.Person" table="PERSONS">
<id name="id">
<generator class="native"/>
</id>
<property name="firstName" column="FIRST_NAME"/>
....
<many-to-one name="address"
class="org.dyndns.orzekanie.RegistrationAddress" lazy="false"
cascade="all" column="ADDRESS_ID"/>
</class>

class Person {
private RegistrationAddress address;
...
}

Sometimes the address for person should be null - there is no address
at all. I would like Hibernate to make possible to delete this address
from database in some way of this:

person.address = null;
hibernateSession.update(person);

The person is a transient object. The foreign key ADDRESS_ID in the
table PERSONS can be null.
I believed that person's address should be automatically deleted from
a database when object address is null and session is updated - but I
get ADDRESS_ID=null in the database but the record ADDRESS is not deleted. I'm wondering how should I tell hibernate
to delete my address from database when person.address is null and session is
updated.

I would be very grateful for any advices,

Pawel


Hibernate version: 3.2.4 sp1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 4.1.22

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 4:33 pm 
Beginner
Beginner

Joined: Wed Jul 11, 2007 7:21 pm
Posts: 21
Location: San Diego, CA
You'd think that would be easy, but oh no. Also, it's poorly documented. I've been dealing with a similar problem myself:

http://forum.hibernate.org/viewtopic.php?t=943167

Hibernate's official answer is here:

http://www.hibernate.org/116.html#A17

_________________
http://rebelheart.squarespace.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 4:58 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
I think that's not exactly the point - there is no Collection in my example. Your suggestion does not work.

It's hard to believe that this case is not serviced by hibernate. Maybe I should make my RegistrationAddress a Collection of one element? But this is rather workaround than the solution of this problem.

Thanks anyway,
Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 6:41 pm 
Beginner
Beginner

Joined: Wed Jul 11, 2007 7:21 pm
Posts: 21
Location: San Diego, CA
Collection or not, you're trying to get a child deleted without having cascade "all-delete-orphan" set, which clearly wont persist the child deletion. Whether the child is a collection or not, it's the same situation.

I still don't have the answer myself; I'm fumbling around right now on a seperate project that is just trying to get this resolved.

If I take an object and remove the items one by one the persistence happens, but if I set the child (in my example, a collection) equal to null, the persistance doesn't carry.

/cry

_________________
http://rebelheart.squarespace.com


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.