-->
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.  [ 13 posts ] 
Author Message
 Post subject: one-to-many where the many can't be deleted; nullable fky
PostPosted: Thu Nov 03, 2005 4:19 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
I have a number of situations in my model where I have a one-to-many and my business logic requires that I don't delete the many just detach it or made "obsolete". For instance I might have a product catalog which has categories and items. A category has many items. The user shops the catalog and attaches items to orders.

The admin can delete items, and categories, but once an item has been attached to an order it is consider fixed and can not be deleted so we always have a record of it. I generally do this where Item would have a categoryId foreign key that is nullable. I would make the categoryid null and possible set an obsolete or inactive property on the item.

I am unclear as to the best way to handle this situation in hibernate. When I delete a category I would like it to cascade and delete all it's items but my definition of delete is to detach and make it inactive.

What is the proper method to handle this common situation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 4:55 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
write an interceptor that sees an action and performs some other action

- or -

write a custom delete function. that when a category is deleted, it fetches all its items, sets them as inactive, makes its categoryid null.

i recommend suggestion #2 as it seems fairly easy.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 5:15 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
kochcp wrote:
write an interceptor that sees an action and performs some other action

- or -

write a custom delete function. that when a category is deleted, it fetches all its items, sets them as inactive, makes its categoryid null.

i recommend suggestion #2 as it seems fairly easy.


So there is a way to implement a custom delete using hibernate with the standard one-to-many mapping, so I can just call session.delete(Category) and it won't delete the item? I am not clear what you are suggesting


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 5:26 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
bluesky wrote:

So there is a way to implement a custom delete using hibernate with the standard one-to-many mapping, so I can just call session.delete(Category) and it won't delete the item? I am not clear what you are suggesting


sorry for being unclear. there is no need to try to customize session.delete to mimic your exact functionality. Write your own delete function. Don't be bound on trying to have hibernate methods do what you need to do.

[code]


public void customDelete (Category cat) {
List items = cat.getItems();

(Iterate over items) {
item1.setInactive();
item1.setCategory(null);
session.merge(item1);

}

//if you actually wanna delete the category, then use your session.delete
session.delete(cat);


}

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 5:38 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
kochcp wrote:
bluesky wrote:

So there is a way to implement a custom delete using hibernate with the standard one-to-many mapping, so I can just call session.delete(Category) and it won't delete the item? I am not clear what you are suggesting


sorry for being unclear. there is no need to try to customize session.delete to mimic your exact functionality. Write your own delete function. Don't be bound on trying to have hibernate methods do what you need to do.

[code]


public void customDelete (Category cat) {
List items = cat.getItems();

(Iterate over items) {
item1.setInactive();
item1.setCategory(null);
session.merge(item1);

}

//if you actually wanna delete the category, then use your session.delete
session.delete(cat);


}



Chris- I see what you mean now. My big concern with this was that when I do a session.delete(category) it will automatically delete the item. So to avoid this would I just set cascade="save-update" like so:

<class name="Category" TABLE="CATEGORIES">
<id name="id" column="ID" type="int" unsaved-value="-1">
<generator class="identity"/>
</id>
<property name="name" column="NAME" not-null="true"/>
<set name="items" cascade="save-update">
<key column="CATEGORY_ID"/>
<one-to-many class="Item"/>
</set>
</class>

So now I can go:

category.getItems().remove(item);
item.setInactive(true);
item.setCategory(null);//IS THIS NECESSARY or will it automatically do this?
session.save(item);
session.delete(cat);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:21 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you are correct that cascade="save-update" will not delete the items from category.

calling delete on category will then run an update on the items and try to null out whatever column you named as your index column in the hbm files for its mapping.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:21 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
*ehh, to make that first sentence more clear,

will not delete the items from the database.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:45 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
kochcp wrote:
*ehh, to make that first sentence more clear,

will not delete the items from the database.


ok great that works thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 8:20 am 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
hi,kochcp
I got a similar problem to ask you, but in my case I do want all the items in a category be deleted when delete this category.
but hibernate always update these items and set the foreign key=null while not delete these items from database.
what can I do?
thanks and regards
Simon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 8:27 am 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
sorry, made a mistake just now
my problem is:
if I delete a item from the category (not cascade delete), it can be deleted from database
for example: session.delete(item);
the hibernate will generate a delete SQL
delete XXX from ..

but when I delete one item and update the category at the same time, hibernate alway generate two update SQL and set the foreign key of item to be null.
for example :
session.update(category);
session.delete(item);
the generated SQL is
update Category ...
update Item set category_id=null, ...

how can I solve this problem
thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 10:38 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you are generating 2 update statements because you are updating the category first, which cascades the update down to the item.

If you think about this in db terms, technically, all you need to do is delete the item from the database because that is the only way hibernate knows it belongs to the category.

I think all you should have to do is:

session.delete(list.remove(index));
session.flush();


try this :)

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 11:01 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
and make sure in your item mapping files that inverse="true", because it lets hibernate know that the same relationship exists on the category side.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 11:04 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
just saw your other thread, it looks like pksiv responded with something similar. did i not wake up early enough for ya ? :)

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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