Why you should avoid CascadeType.REMOVE for to-many associations and what to do instead

  Рет қаралды 11,104

Thorben Janssen

Thorben Janssen

Күн бұрын

Пікірлер: 11
@emersonmorgado
@emersonmorgado 7 жыл бұрын
Hi Thorben, congratulations, and thanks for the explanations, could you share the code, I tried, but instead of remove the relations, I got a "Cannot delete or update a parent row... foreign key..."
@shivendrasaxena2199
@shivendrasaxena2199 3 жыл бұрын
Thanks for the deep insight Sir. Just a query, would using of OrphanRemoval make things any better ?
@lucasterable
@lucasterable 3 жыл бұрын
As far as @OneToMany is concerned: The problem is that Hibernate issues delete statements like this: 'DELETE FROM CHILD WHERE *CHILD_ID* = ?'. If the statements were instead like this: 'DELETE FROM CHILD WHERE *PARENT_ID* = ?', or even better, like this: 'DELETE FROM CHILD WHERE *PARENT_ID IN* (?)' then there would be far fewer statements, and performance would improve without the need to manually optimize the SQL schema :/
@ahmadhosny00
@ahmadhosny00 7 жыл бұрын
Hi Thorben. Thanks for the video! I have one concern regarding the IN clause that takes the IDs that should be removed: In JPQL it's limited to 1000 parameters and this broke my code when the deleted IDs list size exceeded that limit. Does the Native query have the same limitation? I made a workaround by using the DB procedures, which led me to having to adapt for all the differences between the various DBs used by our clients, which was a painful operation to perform and maintain.
@Thorben-Janssen
@Thorben-Janssen 7 жыл бұрын
Hi Ahmad, that depends on your database. Oracle, for example, doesn't allow more than 1000 elements in an IN clause. If you run into that issue, you need to perform the remove operation in multiple batches with
@ahmadhosny00
@ahmadhosny00 7 жыл бұрын
I tried to do that, but in fact the default use case was to delete at least 20k lines and the performance degraded dramatically. We were using Oracle like you said but some other clients preferred MS-SQL Server and I didn't even try to use this approach because of the performance difference between the two. Thanks for the reply ;)
@EsJaviCool
@EsJaviCool 7 жыл бұрын
Hi Thorben, thanks for this very good video. I've been using CASCADE.REMOVE for one to many, many to one, and even for many to many relationships 'cause I transform them into one to many relationships using an identity entity. Do you think I'm taking a good approach to deal with many to many relationships? I'd really appreciate your anwer :)
@Thorben-Janssen
@Thorben-Janssen 7 жыл бұрын
Hi Esteban, It's hard to give a definitive answer to your question without diving deep into the specific application and its use cases. As long as you transform your many-to-many relationships into 2 one-to-many relationships and you don't experience any performance problems, your approach should be ok. Just be aware, that cascade remove doesn't use the most efficient approach to remove the related entities. You might need to refactor that part if you run into performance problems. Regards, Thorben
@EsJaviCool
@EsJaviCool 7 жыл бұрын
Thanks for your answer and advice, I'll take them in account when facing a similar requirement again. Have a good day and thanks a lot for these very useful videos
@arghyamitra3281
@arghyamitra3281 4 жыл бұрын
If cascadeType.remove causing so much problem then why it's there ? Everything in Jpa hibernate is a tweak or hack ... That's why everyone prefer jdbcTemplate
@ylioo
@ylioo 4 жыл бұрын
Been wondering the same. I have to optimize every hibernate query with jpql or native queries
Best Practices for Many-To-One and One-To-Many Association Mappings
9:35
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
How to combine two (or more) Arduino sketches [a MUST watch for new programmers] #arduino
14:00
Programming Electronics Academy
Рет қаралды 2,1 М.
5 ways to initialize lazy associations and when to use them
11:16
Thorben Janssen
Рет қаралды 16 М.
JPA & Hibernate: Basic Annotations You Need To Know
15:11
Thorben Janssen
Рет қаралды 32 М.
How to map an association as a java.util.Map
9:28
Thorben Janssen
Рет қаралды 4,8 М.
Best Practices for Many-to-Many Associations with  Hibernate & JPA
8:36
Persistence APIs - .remove()
20:38
Miss Xing (邢老师)
Рет қаралды 1,3 М.
Hibernate: 6 Mappings to Avoid for High-Performance Applications
10:11
Thorben Janssen
Рет қаралды 10 М.
Persistence With Quarkus Panache
14:48
Sebastian Daschner
Рет қаралды 7 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН