Spring boot - Detached entity passed to persist

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;
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;
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?
19 Replies
JavaBot
JavaBot•10mo ago
āŒ› This post has been reserved for your question.
Hey @Ed! 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.
Peter Rader
Peter Rader•10mo ago
Between relations there are owning-sides and non-owning-sides. Who is the owning side?
Ed
EdOP•10mo ago
@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
dan1st
dan1st•10mo ago
How are you persisting it? Are you using a transaction?
Ed
EdOP•10mo ago
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);
}
@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);
}
Peter Rader
Peter Rader•10mo ago
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.
Ed
EdOP•10mo ago
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
dan1st
dan1st•10mo ago
So the user already exists in the DB?
Ed
EdOP•10mo ago
yeah and the values are valid i checked
dan1st
dan1st•10mo ago
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);
}
@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);
}
Ed
EdOP•10mo ago
this is post authentication, and userdetails is from the AuthenticationPrincipal i can try this 1 sec
dan1st
dan1st•10mo ago
note that the thing with getId() is an example more likely it's getting by name
Ed
EdOP•10mo ago
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();
}
}
@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
dan1st
dan1st•10mo ago
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
Ed
EdOP•10mo ago
well at least now i know why it happened Thanks for the help! 🫔
JavaBot
JavaBot•10mo ago
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.
dan1st
dan1st•10mo ago
I think you might be able to use different methods maybe merge or replace, idk
Ed
EdOP•10mo ago
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
JavaBot
JavaBot•10mo ago
Post Closed
This post has been closed by <@351059014341099521>.

Did you find this page helpful?