-->
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.  [ 2 posts ] 
Author Message
 Post subject: DTO settings for Oracle FK with "on delete cascade" option?
PostPosted: Tue Jul 21, 2015 10:54 am 
Newbie

Joined: Tue Jul 21, 2015 10:29 am
Posts: 2
Hi

I'm dealing with an existing Oracle DB which has two tables looking like :
PARENT ( ID, NAME, .... )
CHILD ( ID, PARENT_ID, NAME, ... )

CHILD.PARENT_ID column has a Foreign Key on PARENT.ID colum with the "On Delete Option" which allows to delete directly entries in PARENT without having to delete explicitely linked entries in CHILD table - Oracle will do it automatically


(the creating scripts looks like :

alter table CHILD
add constraint FK_PARENT_ID foreign key (PARENT_ID)
references PARENT_ID (ID)
on delete CASCADE

)

)

On Java side, I've got so far something like:


public class ChildDTO implements Serializable {
...

@ManyToOne
@JoinColumn(name = "parent_id")
@MapsId("id")
private ParentDTO parent;

...
}




public class ParentDTO implements Serializable {
...

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ChildDTO> childs;

...
}


When deleting an ParentDTO objects, ChildDTO objects related to the ParentDTO are deleted to.
But I can also observe on Oracle side that Hibernate sends a DELETE requests on the table CHILD, though it's not necessary since the "On Delete Cascade" option of my FK make Oracle delete CHILD whenever it's PARENT_ID does not exists anymore in the PARENT table.
So I would like to know the correct option to set , so as Hibernate wont send the DELETE requests on CHILD table anymore, while still handling correctly the ParentDTO <> ChildDTO relationship.

So far I tried this other setting with no success :
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = false)
(apparently not correct on Java side)
and
@OneToMany(mappedBy = "parent", cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH, CascadeType.PERSIST}, orphanRemoval = true)
(didn't seem to change anything)

Thanks in advance!


Last edited by Astrodoudou on Wed Jul 22, 2015 4:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: DTO settings for Oracle FK with "on delete cascade" option?
PostPosted: Wed Jul 22, 2015 4:42 am 
Newbie

Joined: Tue Jul 21, 2015 10:29 am
Posts: 2
OK, I finally found the solution in a book.
The annotation @OnDelete(action= OnDeleteAction.CASCADE) tells Hibernate that the DBMS is doing the cascade and therefore that Hibernate shoud only care of his side.

Here is the ParentDTO with the annotation :


public class ParentDTO implements Serializable {
...

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@OnDelete(action= OnDeleteAction.CASCADE)
private Set<ChildDTO> childs;

...
}


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