#Spring boot - Detached entity passed to persist

35 messages · Page 1 of 1 (latest)

rugged laurel
#

In my UserRequest entity i have the following:

public class UserRequest {

    @OneToOne(cascade = CascadeType.ALL) //i tried persist
    @JoinColumn(name = "user_id")
    private User user;

and in User i have the following:

public class User implements UserDetails {

    @OneToOne(mappedBy = "user")  //i tried without connecting it at all
    private UserRequest userRequest;

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?

undone knollBOT
#

This post has been reserved for your question.

Hey @rugged laurel! Please use /close or the Close 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.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

cold monolith
#

Between relations there are owning-sides and non-owning-sides. Who is the owning side?

rugged laurel
#

@cold monolith 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

fossil shale
#

How are you persisting it? Are you using a transaction?

rugged laurel
#

Its a @Transactional on the method if thats what you mean

#
    @Transactional
    public void deleteAccount(UserDetails userDetails) {
        User user =(User)userDetails;
        user.setUserStatus(UserStatus.TO_BE_DELETED);
        var userRequest= UserRequest.builder()
                .userRequestType(UserRequestType.DELETE_ACCOUNT)
                .requestTimestamp(LocalDate.now())
                .user(user)
                .requestText("")
                .requestStatusType(RequestStatusType.PENDING)
                .build();

        userRequestRepository.save(userRequest);
    }
cold monolith
rugged laurel
#

If you mean i shouldn't have had a @transactional on the method it still doesnt work without it

fossil shale
#

So the user already exists in the DB?

rugged laurel
#

yeah

#

and the values are valid i checked

fossil shale
#

Instead of saving a new object, try first retrieving the object

#

So something like that

    @Transactional
    public void deleteAccount(UserDetails userDetails) {
        User user = userRepository.getById(userDetails.getId());
        user.setUserStatus(UserStatus.TO_BE_DELETED);
        var userRequest= UserRequest.builder()
                .userRequestType(UserRequestType.DELETE_ACCOUNT)
                .requestTimestamp(LocalDate.now())
                .user(user)
                .requestText("")
                .requestStatusType(RequestStatusType.PENDING)
                .build();

        userRequestRepository.save(userRequest);
    }
rugged laurel
#

this is post authentication, and userdetails is from the AuthenticationPrincipal

#

i can try this 1 sec

fossil shale
#

note that the thing with getId() is an example

#

more likely it's getting by name

rugged laurel
#

Indeed it worked but why isnt it directly working from the Authentication Principle

#
 @PostMapping("/deletion")
    public ResponseEntity<?> deleteAccount(@AuthenticationPrincipal UserDetails userDetails) {
        try{
            userRequestServices.deleteAccount(userDetails);
            return ResponseEntity.ok().build();
        }catch (Exception e){

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
#

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

fossil shale
#

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 DB

rugged laurel
#

Thanks for the help!
🫡

undone knollBOT
# rugged laurel 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.

fossil shale
#

I think you might be able to use different methods

#

maybe merge or replace, idk

rugged laurel
#

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