last day (15 days later) » 

7:29 PM
0
A: Spring JPA doesn't update relation

TedEllisIn AdminService your method: public ClassEntity addUserToClass(AdminAddUserToClassModel userAndClassModel) throws UserNotFoundException, ClassNotFoundException, UserAlreadyExistsException { UserEntity user = userService.getUserEntity(userAndClassModel.getUserId()); Cla...

 
doesn't @Service annotation provide a transactional methods? I have a method that do completely the same but with another entity, and it works.
 
There is some good detail on this in this stackoverflow: stackoverflow.com/questions/50833029/… You may also try a saveAndFlush, if you prefer, which I believe should rule out any other issues.
 
Well, ok, I got it. But I added @Transactional annotation to service class and added the class that you wrote, but it's still not working :(
 
Hmm, can we check that the sql you are expecting is being run? In your application.properties spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true Should show you the queries
 
I updated my question with sql queries
 
7:29 PM
I see your update however it is only showing a single select, are you certain that select is coming from the addUserToClass method, as I would expect it to produce multiple update statements
 
So yeah, it's only coming from addUserToClass method. Because I call it in controller when I make a request.
 
I feel like there may be an error occuring after the service calls
UserEntity user = userService.getUserEntity(userAndClassModel.getUserId());
ClassEntity schoolClass = classService.getClassEntity(userAndClassModel.getClassId());
if(schoolClass.getStudents().contains(user)) {
throw new UserAlreadyExistsException("user already in this class");
}
schoolClass.getStudents().add(user);
classRepo.save(schoolClass);
userRepo.save(user);
so userService.getUserEntity would run a select, and the second never gets triggered
if you could break on the ClassEntity line, or throw a few log statements in there, I have a funny feeling they may not be hit
Prefer break points here, but this should do for a test

System.out.println("Test1");
UserEntity user = userService.getUserEntity(userAndClassModel.getUserId());
System.out.println("Test2");
ClassEntity schoolClass = classService.getClassEntity(userAndClassModel.getClassId());
System.out.println("Test3");
if(schoolClass.getStudents().contains(user)) {
throw new UserAlreadyExistsException("user already in this class");
}
schoolClass.getStudents().add(user);
classRepo.save(schoolClass);
userRepo.save(user);
 
7:45 PM
No, actually it hits all the breakpoints :/
 
very strange, these lines:

UserEntity user = userService.getUserEntity(userAndClassModel.getUserId());
ClassEntity schoolClass = classService.getClassEntity(userAndClassModel.getClassId());

both of them should generate a select statement... unless I am missing something here.
and then these lines:

classRepo.save(schoolClass);
userRepo.save(user);

should generate an update.
 
yeah, that's strange. But I also have another method that look exactly like this and it works. So here it is:
```java
public UserEntity addUserToSchool(AdminAddUserToSchoolModel addUserToSchoolData)
throws UserNotFoundException, SchoolNotFoundException, UserAlreadyExistsException {
UserEntity user = userService.getUserEntity(addUserToSchoolData.getUserId());
SchoolEntity school = schoolService.getSchoolEntityByNumber(addUserToSchoolData.getSchoolNumber());
if(school.getStudents().contains(user)) {
 
When you run that it only generates a single select in the terminal as well? I might just have to admit I am in over my head here...
 
Yes, you are right, this method also broken. I don't know why, because it worked just a couple hours ago
 
8:01 PM
I have to run unfortunately but I would set a breakpoint on the .save statements, ensure that your updates are present inside the entities through the debugger. If they ARE present change save to saveAndFlush to force JPA to commit your changes and retry. At that point you should at least have something to work towards.

If your changes are not present in the debugger then you'll have to check why the additions you are making to the entities are not moving over (getStudents().add(user))
I have upvoted your post, hopefully it entices someone else to pop by and take a look. I will pop back in when I get back and see if things have been figured out! Goodluck!
 
well, thank you very much!
 

last day (15 days later) »