Spring boot - Detached entity passed to persist
In my UserRequest entity i have the following:
and in User i have the following:
when i try to save or update User through saving UserReqeust entity i get error:
detached entity passed to persist, how can i fix it through connecting the classes correctly?
19 Replies
ā
This post has been reserved for your question.
Hey @Ed! 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.
Between relations there are owning-sides and non-owning-sides. Who is the owning side?
@Peter Rader owning side is UserRequest, as in, in UserRequest table there is a column user_id that is foreign key to user table
i still cant tell why the entity manager isn't figuring it out automatically, it doesnt happen in any other class
How are you persisting it? Are you using a transaction?
Its a @Transactional on the method if thats what you mean
I think this code is the stranger. You give a entity as parameter. The caller had no Transactional-Scope so the parameter was created in an different transaction that is detached from the transaction of the method deleteAccount.
I am not sure i understand what you mean
If you mean i shouldn't have had a @transactional on the method it still doesnt work without it
So the user already exists in the DB?
yeah
and the values are valid i checked
Instead of saving a new object, try first retrieving the object
So something like that
this is post authentication, and userdetails is from the AuthenticationPrincipal
i can try this 1 sec
note that the thing with
getId()
is an example
more likely it's getting by nameIndeed it worked but why isnt it directly working from the Authentication Principle
could this be caused by authenticationfilter not working correclty?
this is confusing honestly, it works on other parts of the code with no issues only in this entity relationship it causes issues
I think (Spring) JPA doesn't like it if you
save()
an object where another object with the same primary key is already in the DBwell at least now i know why it happened
Thanks for the help!
š«”
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
I think you might be able to use different methods
maybe merge or replace, idk
i tried each and all of them failed
Now looking at other parts of the code, it actually works if you are saving something brandnew fully
as in an entity containing a new entity, it works but if you update something that already exists through a brand new entity the issue appears
either way its working now
Post Closed
This post has been closed by <@351059014341099521>.