Reputation: 63
I am trying to update some user information by passing List of User-Ids as parameter i want to update isActive field of User fo which i am passing the user ids. Below is my controller
@PutMapping
@ResponseStatus(HttpStatus.OK)
@RequestMapping("/UserUpdate")
public ResponseEntity<?> updateUsers(List<Long> userIds) {
**userService.updateUsers(userIds);**
return ResponseEntity.ok(200);
}
updateUsers() is a method in my Service where i have to write the logic I tried something like below but it's not working
public void updateUsers(List<Long> userIds) {
List<Users> userList= userRepository.findAll();
for (Long i : userIds) {
for ( Users user : userList)
{
if(userRepository.findById(i) != null)
{
user.setIsActive(9L);
user.setUserName("Update Test");
}
my dto
public class UserDto {
private List<Users> userList;
private String appName="Users Project";
// getters and setters removed for brevity
And my Users entity class
@Entity
@Table(name="USERS")
public class Users {
@Id
@Column(name="USER_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
@Column(name="NAME")
private String userName;
@Column(name="ACTIVE")
private Long isActive;
// getters and setters removed for brevity
Upvotes: 0
Views: 213
Reputation: 46
public void updateUsers(List<Long> userIds) {
for (Long i : userIds) {
User user = userRepository.findById(i);
if(user != null){
user.setIsActive(9L);
user.setUserName("Update Test");
// call your update method here (this is not stated in your code)
}
}
}
Upvotes: 0
Reputation:
Alternatively you can use the following code
@Modifying
@Query("update Users u set u.isActive = ?1, u.userName = ?2 where u.userId in ?3")
void updateUsers(Long isActive, String userName, List<Long> userId);
Add this code in your userRepository and use the method.
Upvotes: 1