Answer the question
In order to leave comments, you need to log in
How to get a getter from a collection?
I have 2 entities that are linked @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;
}
@Override
public Set<UserRole> findByRole (Set<UserRole> userRole){
return (Set<UserRole>) getSession().get(UserRole.class, (Serializable) userRole);
}
@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);
}
@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
, only it UserRole
is a collection and the method user.getUserRole().getRole()
is not inferred. How can I get getRole()
from a collection? With Department, everything works for me without problems, and as soon as I insert userRole, an error is displayed. And how to write findByRole to a UserRoleDAOImpl
method on a Set collection?
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