SHARE
TWEET

Untitled

a guest Sep 22nd, 2016 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Entity
  2. @Table(name = "users")
  3. @NamedQuery(name = "allUsers", query = "SELECT u FROM User u")
  4.  
  5. public class User {
  6.  
  7. private String username;
  8. private String password;
  9. private String name;
  10. private String surname;
  11. private Department department;
  12. private boolean enabled;
  13. private Set<UserRole> userRole = new HashSet<UserRole>();
  14. private Set<Equipment> equipment = new HashSet<Equipment>();
  15.  
  16. public User() {
  17. }
  18.  
  19. public User(String username, String password, String name, String surname, Department department, boolean enabled,
  20.         Set<UserRole> userRole, Set<Equipment> equipment) {
  21.     super();
  22.     this.username = username;
  23.     this.password = password;
  24.     this.name = name;
  25.     this.surname = surname;
  26.     this.department = department;
  27.     this.enabled = enabled;
  28.     this.userRole = userRole;
  29.     this.equipment = equipment;
  30. }
  31.  
  32. @Id
  33. @Column(name = "username", unique = true, length = 45)
  34. public String getUsername() {
  35.     return this.username;
  36. }
  37.  
  38. public void setUsername(String username) {
  39.     this.username = username;
  40. }
  41.  
  42. @OneToMany(mappedBy = "user")
  43. public Set<UserRole> getUserRole() {
  44.     return this.userRole;
  45. }
  46.  
  47. public void setUserRole(Set<UserRole> userRole) {
  48.     this.userRole = userRole;
  49. }
  50.  
  51. @ManyToOne
  52. @JoinColumn(name = "depId")
  53. public Department getDepartment() {
  54.     return department;
  55. }
  56.  
  57. public void setDepartment(Department department) {
  58.     this.department = department;
  59. }
  60.    
  61. @Entity
  62. @Table(name = "user_roles", uniqueConstraints = @UniqueConstraint(columnNames = { "role", "username" }))
  63. public class UserRole {
  64.  
  65. private Integer userRoleId;
  66. private User user;
  67. private String role;
  68.  
  69. public UserRole() {
  70. }
  71.  
  72. public UserRole(Integer userRoleId, User user, String role) {
  73.     super();
  74.     this.userRoleId = userRoleId;
  75.     this.user = user;
  76.     this.role = role;
  77. }
  78.  
  79. @Id
  80. @GeneratedValue(strategy = GenerationType.IDENTITY)
  81. @Column(name = "user_role_id", unique = true, nullable = false)
  82. public Integer getUserRoleId() {
  83.     return this.userRoleId;
  84. }
  85.  
  86. public void setUserRoleId(Integer userRoleId) {
  87.     this.userRoleId = userRoleId;
  88. }
  89.  
  90. @ManyToOne
  91. @JoinColumn(name = "username")
  92. public User getUser() {
  93.     return this.user;
  94. }
  95.  
  96. public void setUser(User user) {
  97.     this.user = user;
  98. }
  99.  
  100. @Column(name = "role", nullable = false, length = 45)
  101. public String getRole() {
  102.     return this.role;
  103. }
  104.  
  105. public void setRole(String role) {
  106.     this.role = role;
  107. }
  108.    
  109. @Override
  110. public Set<UserRole> findByRole (Set<UserRole> userRole){
  111.     return (Set<UserRole>) getSession().get(UserRole.class, (Serializable) userRole);
  112. }
  113.    
  114. @Override
  115. public void update(User user) {
  116.     String hql = "UPDATE User set department = :department, userRole = :userRole"
  117.             + "WHERE username = :username";
  118.     Query query = getSession().createQuery(hql);
  119.     query.setParameter("department", user.getDepartment());
  120.     query.setParameter("userRole", user.getUserRole());
  121.     query.setParameter("username", user.getUsername());
  122.     int result = query.executeUpdate();
  123.     getSession().update(user);
  124. }
  125.    
  126. @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
  127. private ModelAndView updateUser(@ModelAttribute("users") User user, BindingResult result) {
  128.     ModelAndView model = new ModelAndView();
  129.     department = departmentService.findByDepartmentName(user.getDepartment().getName());
  130.     user.setDepartment(department);
  131.     userRole = userRoleService.findByRole(user.getUserRole());
  132.     user.setUserRole(userRole);
  133.     userService.update(user);
  134.     model.addObject("allUsers", userService.getAllUsers());
  135.     model.setViewName("superAdmin");
  136.     return model;
  137. }
  138.    
  139. org.hibernate.TypeMismatchException: Provided id of the wrong type for class az.nikoil.model.UserRole. Expected: class java.lang.Integer, got class java.util.HashSet
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top