Spring JPA
I am tryna Delete the forgotPassword Enetity if the Otp expired time is passed out and that is 10 sec right now but after the Otp time expired the otp entity is not deleting itself from Db . Here is the implemet Can anyone help
82 Replies
⌛
This post has been reserved for your question.
Hey @Danix! 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 closed after 300 minutes of inactivity.
@dan1st | Daniel can u see it
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
💤
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.
I changed it but facing the same thing again !
i cant find any delete call here @AJediMasterIam
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
this
i will delete the forgot password entity from db after expiration
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
i did it
and getting the print like Deleting ForgotPassword entity with id: 1 and Deleted ForgotPassword entity with id: 1
but not deleted the data from DB
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Where ?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
i think i did the same think ?
This is the sql
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
yehh but whats the issue i am getting bcz the query is right i think so where the issue should be ?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
when i was making the forgot password table in the Db then i made it cascade delete
My Forgot Password Table says
@dan1st | Daniel can u see the issue is not removing
What does
forgetPasswordRepository.deleteById
return?
it sais an error occured. Where? Is there a stack trace? Are you printing that?nothing just void
yeh i am printing it
Can you show that part?
After printing the
The Time is Expired
the error is thrown i think stack is from thereUnknown User•5mo ago
Message Not Public
Sign In & Join Server To View
The Repo class The Service class method
Here it is been used
Can you show the full stack trace?
you are getting some exception
yehh here is the stack trace
💤
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.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
that ^
hmm so should i have to throw an exception at that point or not ?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
it's also possible to configure it to commit the transaction on that exception
I think using
noRollbackFor
or similarThe issue is due to relatioship guys. User had:
And
ForgotPassword
:
He can delete User and cascade to ForgotPassword
But he can't delete ForgotPassword
because of the reference.
He tries, but it doesn't happenUnknown User•5mo ago
Message Not Public
Sign In & Join Server To View
I solved him the issue. And explained. Also learned myself. See full class:
Method:
public String verifyOtp(@RequestParam String otp , Model model , HttpSession session)
line 126Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
No it doesn't
I already tried
I had to nullify the reference by
userid.setForgotPassword(null);
It deletes the entity and no matter that it throws an exceptionUnknown User•5mo ago
Message Not Public
Sign In & Join Server To View
his exception
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Nah he has this single transaction in that method.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Bye
I believe in your statements. Usually it's true.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Then why when on this method we have
@Trabsactional
and we delete the reference from user
, save it to database, then delete the forgetPassword
entity and at the end we throw an exception then I check the databse and see that it's deleted?Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Hibernate deleted it after the exception was thrown:
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
it depends on how that method is called though
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
When I removed
@Transactional
annotation from the method, the entity was still removed.
But not after the exception, but before it.it depends on the exception. Not all exceptions are causing a rollback IIRC
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
yes
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
e.g. I think
ResponseStatusException
s don't cause rollbacksNow tell me how to turn on transaction logging.
we have only these:
Stack Overflow
Showing a Spring transaction in log
I configured spring with transactional support. Is there any way to log transactions just to ensure I set up everything correctly? Showing in the log is a good way to see what is happening.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
transactions are rolled back on unchecked exceptions (by default)
cc @IncognitoBurrito @Tomasm21
His exception that is thrown is this:
which is rolled back if not stated otherwise
I don't know wheret it is stated otherwise.
so the exception undoes the deletion
which I think was initially stated by IncognitoBurrito
Danix was upset because he couldn't delete the entity anyhow. I read the info that in such a case we can manually nullify the reference in the parent entity and save it. Then we can delete the child entity. So the key was:
But now from experienced users I hear that it's nonsense and due to thrown exception the deletion cannot happen.
The rollback should occur.
If you are in the mood to investigate it and to prove that something special happened then you can invite me.
such a rollback literally ensures the entity not being deleted
and according in the stack trace, I don't see anything about the cascading issue as of when the OP posted the stack trace
I have made Spring Console output. Currently deleting ugly looking symbols and soon will log you here. It's when transaction log is on.
I don't know it. I learn it. Maybe you as more experienced will explain me.
There:
The OP took my edited code. And is happy with it.
Now the discussion object is that deletion cannot happen. The exception is not checked.
That seems like there's no
@Transactional
active therePerhaps. I removed the annotation:
Then you can't expect to reproduce what the OP is getting
Ok. I will put the annotation back. And show the console.
There:
In both cases the entity was deleted. And I don't know of the rollback that should occur. Should I see it in the console output?
that's because the whole thing is wrapped in a try-catch block I think
but idk their code
I had to put in a lot of efforts to get the rollback to work:
I had to show specific criteria for the rollback:
Then throw this unchecked exception in the
try
block :
and to catch it in the catch
block and to re-throw it:
Only then my transaction was rolled back and then entity was put back and undeleted.
Damn..
lolThe
rollbackOn = OtpNotFoundExcpetion.class
wouldn't be necessary and just removing the catch(Exception)
block would probably be sufficientYou are right. The rollback really occurs if there are no any try-catch blocks and with a single
@Transactional
annotation. Hibernate doesn't initiate the deletion at all.
Then when we throw such unchecked exception then it rolls back.
Now I will rememebr how to do it.
But since our task is to show error messages in thymeleaf templates we will throw custom exceptions and will use try catch blocks.
And we need to delete entity in specific circumstances.
Thank you.
This thread is over.
To sum up everything, I see that nor @Transactional
nor the throw of the exception and the rollback are not factors that prevented Danix way to delete the entity. If we don't do an exception and return a string (template) and we assign the error message model.addAttribute("error", "Otp Expired");
then the way Danix deletes his entity actually doesn't work here:
emailService.deleteForgotPasswordById(fId)
:
This doesn't make the ForgotPassword
entity to be deleted.💤
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.