Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "user", schema = "rps_am")
- @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id", scope = UserEntity.class)
- public class UserEntity {
- @ApiModelProperty(required = true, hidden = true)
- private Long id;
- private String logonId;
- private String emailAddress;
- private String password;
- private Collection<UserRoleEntity> userRolesById;
- @Id
- @Column(name = "ID")
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- @Basic
- @Column(name = "PASSWORD")
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Basic
- @Column(name = "EMAIL_ADDRESS")
- public String getEmailAddress() {
- return this.emailAddress;
- }
- public void setEmailAddress(String emailAddress) {
- this.emailAddress = emailAddress;
- }
- @Basic
- @Column(name = "LOGON_ID")
- public String getLogonId() {
- return logonId;
- }
- public void setLogonId(String logonId) {
- this.logonId = logonId;
- }
- @OneToMany(mappedBy = "userByUserId", fetch=FetchType.EAGER) //If I make it LAZY my problem is sloved.
- @Fetch(FetchMode.JOIN)
- @Transactional
- @JsonIgnore
- public Collection<UserRoleEntity> getUserRolesById() {
- return userRolesById;
- }
- public void setUserRolesById(Collection<UserRoleEntity> userRolesById) {
- this.userRolesById = userRolesById;
- }
- private List<RoleEntity> roles;
- @Transient
- public List<RoleEntity> getRoles() {
- if (roles == null) {
- Collection<UserRoleEntity> c = getUserRolesById();
- roles = new ArrayList<>();
- for (UserRoleEntity userRoleEntity : c) {
- roles.add(userRoleEntity.getRoleByRoleId());
- }
- }
- return roles;
- }
- public void setRoles(List<RoleEntity> roles) {
- this.roles = roles;
- }
- }
- @Entity
- @Table(name = "role", schema = "rps_am")
- @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id", scope = RoleEntity.class)
- public class RoleEntity {
- @ApiModelProperty(required = true, hidden = true)
- private Long id;
- private String roleName;
- private Collection<UserRoleEntity> userRolesById;
- @Id
- @Column(name = "ID")
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- @Basic
- @Column(name = "ROLE_NAME")
- public String getRoleName() {
- return roleName;
- }
- public void setRoleName(String roleName) {
- this.roleName = roleName;
- }
- @JsonIgnore
- @OneToMany(mappedBy = "roleByRoleId")
- public Collection<UserRoleEntity> getUserRolesById() {
- return userRolesById;
- }
- public void setUserRolesById(Collection<UserRoleEntity> userRolesById) {
- this.userRolesById = userRolesById;
- }
- }
- @RequestMapping(value = "userrole", method = RequestMethod.POST)
- public UserRoleEntity create(@RequestBody UserRoleEntity obj) {
- UserEntity usr = userRepository.findById(obj.getUserId());
- RoleEntity role = roleRepository.findById(obj.getRoleId());
- obj.setRoleByRoleId(role); // I added one role for specific user e.g HR (assume user already has role ADMIN)
- obj.setUserByUserId(usr);
- UserRoleEntity rc = userRoleRepository.saveAndFlush(obj); // now after this line user has role ADMIN and HR
- UserEntity user = userRepository.findByLogonId(obj.getLoginId);
- 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
- ...
- ...
- return rc;
- }
Add Comment
Please, Sign In to add comment