-->
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.  [ 5 posts ] 
Author Message
 Post subject: many to one issues
PostPosted: Sat May 13, 2006 1:58 am 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Hibernate version: 3.1

Hello

This is a general question.

I have a User object and some other objects such as Comment, BlogEntry, ForumPost etc. All of the later objects keep a reference to a single User object, which represents the creator.
I've mapped this reference to the creator using a many-to-one hibernate relation pointing to the unique id of the user. Everything works well, until I delete a User (e.g user with id 23) who has already created some Comment, BlogEntry etc object. When I try to get any of those objects, which still point to user 23, I get:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [User#23]

I would like to get a null reference for User inside my Comment/BlogEntry object instead of this exception.
What should I do?

Should I 'nullify' all references to user 23 before deleting him? Should I do that myself (using a database trigger, on the java level) or is there a more elegant way so hibernate can handle this logic for me?

Note that I do not want on delete cascade because deleting user 23 would delete his comments/entries as well.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 12:19 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
You can create a foreign key in your children tables (comment, post etc.) and set it to 'on delete set null'. This way, when a user is deleted, user_id column is set to null.

You may face another problem : if Hibernate has cached items belonging to this user, Hibernate will be aware that they have be updated (user set to null). In this case Hibernate will query the DB for this use and will report an exception (since it as been deleted).

If you want to avoid this, my advice is to nullify all user fields at Java level.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 12:47 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
thanks for your reply

This 'on delete set null' thing, how do I do it? On the DB level? With a trigger?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 4:54 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
cherouvim wrote:
This 'on delete set null' thing, how do I do it? On the DB level? With a trigger?

On the DB level, when you create the FK.

Kind Regards,

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 2:57 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Just to let you know that what I wanted was the not-found="ignore" property on the <many-to-one relation.

thanks


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