-->
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.  [ 6 posts ] 
Author Message
 Post subject: One-to-many question and help needed on callback methods
PostPosted: Thu Feb 08, 2007 4:37 am 
Newbie

Joined: Wed Oct 26, 2005 3:37 am
Posts: 15
Hello!

I guess this question might have been asked a lot but I just couldn't find where.
So, I have one-to-many relation between Data and Link (one Data - many Links). I want to ensure that when all the Links that reference a Data are deleted, the Data will be removed as well.
Is there a way to declare such behaviour in Hibernate?
I tried to use callback method on remove of Link and check whether the linked Data still has more Links but I was not able to get the callback method called.
I have the following data model:

LinkBase class
Link extends LinkBase (Joined inheritance) and has a Data

I have the following method in both Link and LinkBase:
Code:
    @PreRemove
    public void onDelete()

The code that I actually test with is:
Code:
LinkBase base = findById(...);
currentSession.delete(base);


It seems I have 2 questions in this topic and I think I must be missing something really basic for both of them.

Any help is appreciated!
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 5:39 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Guess you should post more info, if you want anybody to help you. Hibernate version, mappings and so on ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 5:48 am 
Newbie

Joined: Wed Oct 26, 2005 3:37 am
Posts: 15
polarbaerhh wrote:
Guess you should post more info, if you want anybody to help you. Hibernate version, mappings and so on ...


Hibernate Core 3.2.2
Hibernate Annotations 3.2.1

There is nothing special in mapping. Actually I am asking how to make the mapping so that non-associated objects on the One side of the relations get deleted by Hibernate, if this is possible at all.
On the callback part:

Code:
@Entity
@Table(name="...")
@Inheritance(strategy=InheritanceType.JOINED)
public class LinkBase {
...
@PreRemove
public void OnDelete() {
...
}
}

@Entity
@Table(name="...")
public class Link extends LinkBase {

@ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="data_id")
public Data getData() {
....
}


@PreRemove
public void OnDelete() {
...
}
}


It seems pretty straightforward to me.
Of course, I will post any other part that could help solve my problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 1:02 pm 
Beginner
Beginner

Joined: Wed Jan 03, 2007 7:47 pm
Posts: 23
Location: Richardson, Texas
Jana,

There is CascadeType.Remove as well, did you try this.

Regards,
Kurt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 1:25 pm 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
cascade="all-delete-orphan"

delete-orphan will make sure children are removed when the parent is removed.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 3:41 am 
Newbie

Joined: Wed Oct 26, 2005 3:37 am
Posts: 15
Thanks for the answers but what I want is:

When all children are removed, the parent to be removed too. That is, no parent to exist without children.
I know about delete_orphan bit I want the other way around.

Currently I have implemented this with interceptor that checks children being deleted and if this is the last child, deletes the parent.
But I am not sure this is the proper way to go.


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