Guest User

Untitled

a guest
Nov 6th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. @Entity
  2. @Table(name = "user", schema = "rps_am")
  3. @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id", scope = UserEntity.class)
  4.  
  5. public class UserEntity {
  6. @ApiModelProperty(required = true, hidden = true)
  7. private Long id;
  8. private String logonId;
  9. private String emailAddress;
  10. private String password;
  11. private Collection<UserRoleEntity> userRolesById;
  12. @Id
  13. @Column(name = "ID")
  14. @GeneratedValue(strategy = GenerationType.AUTO)
  15. public Long getId() {
  16. return id;
  17. }
  18. public void setId(Long id) {
  19. this.id = id;
  20. }
  21. @Basic
  22. @Column(name = "PASSWORD")
  23. public String getPassword() {
  24. return password;
  25. }
  26. public void setPassword(String password) {
  27. this.password = password;
  28. }
  29.  
  30. @Basic
  31. @Column(name = "EMAIL_ADDRESS")
  32. public String getEmailAddress() {
  33. return this.emailAddress;
  34. }
  35. public void setEmailAddress(String emailAddress) {
  36. this.emailAddress = emailAddress;
  37. }
  38. @Basic
  39. @Column(name = "LOGON_ID")
  40. public String getLogonId() {
  41. return logonId;
  42. }
  43. public void setLogonId(String logonId) {
  44. this.logonId = logonId;
  45. }
  46. @OneToMany(mappedBy = "userByUserId", fetch=FetchType.EAGER) //If I make it LAZY my problem is sloved.
  47. @Fetch(FetchMode.JOIN)
  48. @Transactional
  49. @JsonIgnore
  50. public Collection<UserRoleEntity> getUserRolesById() {
  51. return userRolesById;
  52. }
  53. public void setUserRolesById(Collection<UserRoleEntity> userRolesById) {
  54. this.userRolesById = userRolesById;
  55. }
  56. private List<RoleEntity> roles;
  57. @Transient
  58. public List<RoleEntity> getRoles() {
  59. if (roles == null) {
  60. Collection<UserRoleEntity> c = getUserRolesById();
  61. roles = new ArrayList<>();
  62. for (UserRoleEntity userRoleEntity : c) {
  63. roles.add(userRoleEntity.getRoleByRoleId());
  64. }
  65. }
  66. return roles;
  67. }
  68. public void setRoles(List<RoleEntity> roles) {
  69. this.roles = roles;
  70. }
  71. }
  72.  
  73.  
  74.  
  75. @Entity
  76. @Table(name = "role", schema = "rps_am")
  77. @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id", scope = RoleEntity.class)
  78. public class RoleEntity {
  79. @ApiModelProperty(required = true, hidden = true)
  80. private Long id;
  81. private String roleName;
  82. private Collection<UserRoleEntity> userRolesById;
  83.  
  84. @Id
  85. @Column(name = "ID")
  86. @GeneratedValue(strategy = GenerationType.AUTO)
  87. public Long getId() {
  88. return id;
  89. }
  90.  
  91. public void setId(Long id) {
  92. this.id = id;
  93. }
  94.  
  95. @Basic
  96. @Column(name = "ROLE_NAME")
  97. public String getRoleName() {
  98. return roleName;
  99. }
  100.  
  101. public void setRoleName(String roleName) {
  102. this.roleName = roleName;
  103. }
  104.  
  105. @JsonIgnore
  106. @OneToMany(mappedBy = "roleByRoleId")
  107. public Collection<UserRoleEntity> getUserRolesById() {
  108. return userRolesById;
  109. }
  110.  
  111. public void setUserRolesById(Collection<UserRoleEntity> userRolesById) {
  112. this.userRolesById = userRolesById;
  113. }
  114. }
  115.  
  116. @RequestMapping(value = "userrole", method = RequestMethod.POST)
  117. public UserRoleEntity create(@RequestBody UserRoleEntity obj) {
  118.  
  119. UserEntity usr = userRepository.findById(obj.getUserId());
  120. RoleEntity role = roleRepository.findById(obj.getRoleId());
  121. obj.setRoleByRoleId(role); // I added one role for specific user e.g HR (assume user already has role ADMIN)
  122. obj.setUserByUserId(usr);
  123. UserRoleEntity rc = userRoleRepository.saveAndFlush(obj); // now after this line user has role ADMIN and HR
  124. UserEntity user = userRepository.findByLogonId(obj.getLoginId);
  125. Collection<UserRoleEntity> urs = user.getUserRolesById();// after execution of this line i am getting only ADMIN as a role I am not able to get HR
  126. ...
  127. ...
  128. return rc;
  129. }
Add Comment
Please, Sign In to add comment