Spring CrudRepository how to implement update action?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
39 views
Not updated entity using the method repository.save () here is my update method:
@Transactional @RequestMapping(method = RequestMethod.PUT path = "/{id}") public @ResponseBody String update(@PathVariable(value = "id") String id, @RequestParam(required = false) String login, @RequestParam(required = false) String name, @RequestParam(required = false) String password) { Optional user = userRepository.findById(Long.valueOf(id)); if (user.isPresent()){ User u = user.get(); if (login != null) u.setLogin(login); if (name != null) u.setUsername(name); if (password != null) u.setPassword(password); userRepository.save(u); return "{ status : success }"; } return "{ status : error }"; }


Repository:
package spp.lab.reposository; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import spp.lab.models.User; @Repository public interface UserRepository extends CrudRepository { }


Perhaps someone faced with a similar problem.
by | 39 views

1 Answer

0 like 0 dislike
It's simple - forget the "flush" change. After `userRepository.save(u);` run `userRepository.flush();` or one-hop `userRepository.saveAndFlush(u);`
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users