Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One to One delete on cascade

I have 2 classes: Entry and Content.

public class Entry implements Serializable {
     @OneToOne(cascade=CascadeType.ALL)
     private Content content;
}

And Content have his values (name etc etc etc) ... What I want to do is if a content its deleted I want to delete any Entry ho its related with ...

This its not the tipical orphan problem (Im not deleting an Entry! Im deleting the Content)

Entry can have or not a content related with ... and the contents existenc are independient of the entrys ...

How can I say to hibernate "Hey ... if u are going to delete a content find any related FK and delete it as well!"

like image 569
BrunoX Avatar asked Nov 13 '22 10:11

BrunoX


1 Answers

Why not creating a link from Content to Entry as follows:

public class Content implements Seralizable {
    @OneToOne(cascade=CascadeType.REMOVE, optional=true)
    private Entry entry;
}

Please note the optional attribute. This represents the fact that a Content might not have an Entry related to it as you said in your post.

like image 96
Gonzalo Garcia Lasurtegui Avatar answered Jan 06 '23 08:01

Gonzalo Garcia Lasurtegui