-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping.Delete.Cascade?!
PostPosted: Thu Jul 27, 2006 1:21 pm 
Newbie

Joined: Wed Oct 19, 2005 8:04 am
Posts: 19
Suppose i have 2 tables, that are related:

One is Terminal table and has following mapping to another table called TerminalModel:

<many-to-one name="terminalModel" lazy="false"
class="TerminalModel"
column="termmodel_id"
cascade="none"/>

And happened that i need to delete a row from TerminalModel, but if i delete it is gone but termmodel_id in Terminal table stays same and i get an error while getting list of Terminals, since related object from TerminalModel with that ID doesn't exist anymore. So my question is, how can i delete object from table TerminalModel so it sets termmodel_id in parrent table Terminal to null but dont delete the whole row?

Thank u!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 1:36 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
terminal.setTerminalModel(null);
session.delete(terminalModel);
session.flush();

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 6:23 am 
Newbie

Joined: Wed Oct 19, 2005 8:04 am
Posts: 19
That is too easy, but imagine i have 10000 Terminals, then i will have to get list of all those terminals that have that TerminalModel and run


Code:
terminal.setTerminalModel(null);


for every single terminal and that is insane to run it 10000 times...
There must be some easy way Hibernate manages it.
Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 7:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
welcome to pointer chasing ;)

This is the price you "pay" to be able to unittest your domainmodel without a "persistence container". Your code will work both with and without hibernate.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 9:04 am 
Newbie

Joined: Wed Oct 19, 2005 8:04 am
Posts: 19
Didn't really understand what u did ment by that :P


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
just telling you that hibernate does not mess with your associations and we do not do "container managed relationships".

If we did automatically do this you would not be able to use your domaincode in e.g. unittests without hibernate.

Plus what about other objects that is pointing to this object? Should hibernate load them all automatically and null them out ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 9:32 am 
Newbie

Joined: Wed Oct 19, 2005 8:04 am
Posts: 19
I just asked, cuz i am tottaly newb to Hibernate, so u don't have to blame me for not knowing such things.
But thanks anyways :P
I just wanted to check if there is any easier way.
Cuz it could have been like that, for example u put into mapping some extra data that tells what parrent tables refer to this table objects, and if this object is deleted hibernate automaticaly nulls parrent table keys values up by calling one update (update parrent table set refer_key=null where refer_key=deleted_object_key). But if there is no smth like that, i am fine with that and i know that there is no easier way :P
I will do it manualy :P


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 1:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no blame placed - just trying to explain it by letting you think about what you are actually asking for ;)

_________________
Max
Don't forget to rate


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