I have a service that gets Equipment by username . And there is a method in the controller that displays the roles Manager and Users in View Equipment .

Equipment Model:

 @Entity @Table(name = "equipment") public class Equipment { private int id; private String brand; private String model; private String inventoryNumber; private String serialNumber; private String type; private String note; private User user; 

Users model:

 @Entity @Table(name = "users") @NamedQuery(name = "allUsers", query = "SELECT u FROM User u") public class User { private String username; private String password; private String name; private String surname; private Department department; private boolean enabled; private Set<UserRole> userRole = new HashSet<UserRole>(); private Set<Equipment> equipment = new HashSet<Equipment>(); 

Controller Method:

 @RequestMapping(value = "/manager", method = RequestMethod.GET) private ModelAndView manager() { ModelAndView model = new ModelAndView(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); User user = userService.findByName(name); String username = user.getUsername(); List<Equipment> managerEquip = equipmentService.findByUser(username); Department department = user.getDepartment(); List<User> depUsers = userService.findByDepartment(department); for (User users : depUsers){ String depUsername = users.getUsername(); userEquip = equipmentService.findByUser(depUsername); } model.addObject("userEquip", userEquip); model.addObject("managerDep", managerEquip); model.setViewName("manager"); return model; } 

In the role of Manager the Equipment list in the view is displayed normally, and in the role of the Users too, if you enter it by hand, it displays it. And the username he correctly finds everything. Separately deduced logins, everything works. But when I need to display all Users , it does not display. I think the problem is in the loop. It finds equipmentService.findByUser(depUsername); only by last username . How can I make the loop write down from above each time, instead of overwriting it on the previous record?

  • I read 3 times, but did not understand what and where you are displayed. - Vartlok
  • Made corrections. Now it is clear? - Farik013
  • I do not see where you set the userEquip variable userEquip be global? If so, then you have a bad non-thread safe application. In general, if I understood correctly, then you need a separate list (for example, ArrayList) and add it there via addAll - Vartlok
  • private List <Equipment> userEquip; - Farik013
  • @Vartlok I tried to do it this way, but then I get an error NullPointException. I don’t know exactly how to create a separate list and how to add it. I thought in this direction, but it did not work out. Can you help with this and the problem will be solved? - Farik013

0