Answer the question
In order to leave comments, you need to log in
Spring, CrudRepository, how to implement update action?
Entities are not updated using the repository.save() method, 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> 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 }";
}
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<User, Long> {
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question