@Farik013

Как получить getter из коллекции?

У меня имеется 2 сущности, которые связаны @ManyToOne.

@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>();

public User() {
}

public User(String username, String password, String name, String surname, Department department, boolean enabled,
        Set<UserRole> userRole, Set<Equipment> equipment) {
    super();
    this.username = username;
    this.password = password;
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.enabled = enabled;
    this.userRole = userRole;
    this.equipment = equipment;
}

@Id
@Column(name = "username", unique = true, length = 45)
public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

@OneToMany(mappedBy = "user")
public Set<UserRole> getUserRole() {
    return this.userRole;
}

public void setUserRole(Set<UserRole> userRole) {
    this.userRole = userRole;
}

@ManyToOne
@JoinColumn(name = "depId")
public Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}

И:

@Entity
@Table(name = "user_roles", uniqueConstraints = @UniqueConstraint(columnNames = { "role", "username" }))
public class UserRole {

private Integer userRoleId;
private User user;
private String role;

public UserRole() {
}

public UserRole(Integer userRoleId, User user, String role) {
    super();
    this.userRoleId = userRoleId;
    this.user = user;
    this.role = role;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_role_id", unique = true, nullable = false)
public Integer getUserRoleId() {
    return this.userRoleId;
}

public void setUserRoleId(Integer userRoleId) {
    this.userRoleId = userRoleId;
}

@ManyToOne
@JoinColumn(name = "username")
public User getUser() {
    return this.user;
}

public void setUser(User user) {
    this.user = user;
}

@Column(name = "role", nullable = false, length = 45)
public String getRole() {
    return this.role;
}

public void setRole(String role) {
    this.role = role;
}

UserRoleDAOImpl:

@Override
public Set<UserRole> findByRole (Set<UserRole> userRole){
    return (Set<UserRole>) getSession().get(UserRole.class, (Serializable) userRole);
}


DepartmentDAOImpl:
@Override
public Department findByDepartmentName(String departmentName) {
    List<Department> departments = getSession()
            .createQuery("select d from Department d where d.name =:departmentName")
            .setParameter("departmentName", departmentName).list();
    return departments.size() == 0 ? null : departments.get(0);
}


Update метод:

@Override
public void update(User user) {
    String hql = "UPDATE User set department = :department, userRole = :userRole"
            + "WHERE username = :username";
    Query query = getSession().createQuery(hql);
    query.setParameter("department", user.getDepartment());
    query.setParameter("userRole", user.getUserRole());
    query.setParameter("username", user.getUsername());
    int result = query.executeUpdate();
    getSession().update(user);
}

И отрывок контроллера:

@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
private ModelAndView updateUser(@ModelAttribute("users") User user, BindingResult result) {
    ModelAndView model = new ModelAndView();
    department = departmentService.findByDepartmentName(user.getDepartment().getName());
    user.setDepartment(department);
    userRole = userRoleService.findByRole(user.getUserRole());
    user.setUserRole(userRole);
    userService.update(user);
    model.addObject("allUsers", userService.getAllUsers());
    model.setViewName("superAdmin");
    return model;
}

Вопрос состоит в том, что метод

userRole = userRoleService.findByRole(user.getUserRole());

не работает. Все идентично как с department, только UserRole является коллекцией и метод user.getUserRole().getRole()не выводится. Как я могу получить getRole() из коллекции? С Department у меня все работает без проблем, а как только вставляю userRole, выводится ошибка. И как написать findByRole в UserRoleDAOImpl метод для коллекции Set?
  • Вопрос задан
  • 150 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы