Bypass SQLRestriction to achieve SoftDelete with "hard delete"
I have this entity:
11 Replies
⌛
This post has been reserved for your question.
Hey @circle! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
And in the product service layer:
Tried deleting with this repository:
Repository's
.delete
and .deleteAll
only works with the filtered SQLRestriction. Thus, it will only delete products where deleted = false
.
That is not what I want. So I added custom queries:
- deleteProductPermanently
- deleteAllProductsPermanently
, but it also throws:
Or basically DataIntegrityViolationException that I thought it shouldn't happen since I've already used CascadeType.ALL
and orphanRemoval in one-to-many relations within Product entity.
This is just like using SoftDelete where I can't bypass it and Hard Delete the soft-deleted ones
Is it the trade-off?
The other solution would probably updating each products, or the deleted ones with soft-deletion = false
so that it will also be picked up by the native .delete
and .deleteAll
THAT ALSO DOES NOT WORK
I think it's the SQLRestriction making it hard
I might need to manually do all the filtrations 😭did you try writing a query without the query annotation
like deletedWhereDeletedTrue
so jpa generates it automatically
or deleteByIdAndDeleted
i think the latter is the correct format
and if u dont wanna do by id do deleteByDeleted
Let me try
For some cases yes
I think the SQLRestriction still restricts any query coming in :/
Tried
deleteByProductIdAndDeletedTrue
. I have yet found any way to bypass the SQLRestrictionSo in a nutshell when you try to delete product, you get a foreign key violation because variant for some reason is not being deleted?
did you try using the @where annotation instead of sql restriction
also you should probably mark the deleting method with transactional
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
It does happen yes
It is deprecated since JPA hibernate 6.4
Also did that, I put @Transactional on the service layer
Added the question to stackoverflow:
https://stackoverflow.com/questions/79175803/bypassing-sqlrestriction-on-entities-for-jpa-queries
Stack Overflow
'Bypassing' @SQLRestriction on entities for JPA queries
Background
I'm implementing both soft and hard delete functionality in a JPA/Hibernate application. Given an entity with SQLRestriction defined to restrict every query within the JPA to return non ...
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.