0

so I have an entity called "User"

@Entity(name = "user")
@Audited
public class User extends DataObjectAudit {

    private static final long serialVersionUID = 1L;

    private Set<UserProjectCenterRole> roles = new HashSet<UserProjectCenterRole>();
    private RoleTypeEnum role = null;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, orphanRemoval = true)
    @Cascade(value = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DELETE, CascadeType.SAVE_UPDATE })
    @Filters({ @Filter(name = "deletedFilter", condition = "deleted <> :deletedParam") })
    @NotAudited
    public Set<UserProjectCenterRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<UserProjectCenterRole> roles) {
        this.roles = roles;
    }

    @Transient
    public RoleTypeEnum getRole() {
        return role;
    }

    @Transient
    public void setRole(RoleTypeEnum role) {
        this.role = role;
    }

}

And here the UserProjectCenterRole entity:

@Entity
@Table(name = "user_projectcenter_role")
public class UserProjectCenterRole extends DataObjectAudit {

    private static final long serialVersionUID = 1L;

    private User user = null;
    private ProjectCenter projectCenter = null;
    private RoleTypeEnum role = null;
    private Boolean active = null;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    public User getUser() {
        return user;
    }

    @Basic
    public Boolean getActive() {
        return active;
    }

    @Enumerated(EnumType.STRING)
    public RoleTypeEnum getRole() {
        return role;
    }
}

And in the panel, there is a drop down panel where the current active role of the user is loaded among with the "lower" possible roles. The options are the result of getRoles() and the chosen option should be loaded into the "role" property.

User sessionUser = StudySession.getSessionUser();
        List<RoleTypeEnum> roles = new ArrayList<RoleTypeEnum>();
        role.addAll(sessionUser.getActiveRole().getLowerAndEqualsThanSelf());

        WefDropDownPanel<RoleTypeEnum> role = 
                new WefDropDownPanel<RoleTypeEnum>(helper.of(RoleTypeEnum.class, "role").errorRequired(), //
                roles) //
                .setSizes(Size.S0, Size.S1, Size.S4);
        add(roles);

Then the onBeforeSave method for the panel is:

@Override
    protected void onBeforeSave(AjaxRequestTarget target, WefForm<User> form) {
        super.onBeforeSave(target, form);

        User user = getModelObject();

        Set<UserProjectCenterRole> roles = user.getRoles();
        UserProjectCenterRole currentRole = null;
        if (!roles.isEmpty()) {
            currentRole = roles.iterator().next();
        }
        else {
            currentRole = new UserProjectCenterRole();
            roles.add(currentRole);
        }

        currentRole.setRole(user.getRole());
        currentRole.setUser(user);
        currentRole.setActive(true);
    }

But at that point "getRole" returns null... and I can't guess why...

diminuta
  • 1,545
  • 8
  • 32
  • 55
  • Looking at your `User` class I'm confused: is `roles` a `Set` or a `RoleTypeEnum`? Are you sure you haven't made a typo and you're using the wrong variable as a result (i.e. `roles` instead of `role`)? I also can't see a `getRole()` method in the `User` class. – JonK May 26 '14 at 09:02
  • About the getRole(): it was a typo, I have corrected the code (it's not in english...). Roles is indeed a Set, but we are using a RoleTypeEnum as the result for the DropDownChoice... to build the DropDownChoice, the roles are iterated and their "role" property is added to a list to be the source for the DropDownChoice. When the user chooses one option, then it is converted to a UserProjectCenterRole and added to the user roles list. I haven't written this code, I am the one who is supposed to fix it... – diminuta May 26 '14 at 09:20
  • Given that it's marked as `@Transient`, when and where should `role` get set to anything other than `null`? – JonK May 26 '14 at 09:25
  • When the user selects something from the DropDownChoice. The thing is that when you create the user, the chosen role is saved. But when you edit the user, it is null. So, in the onBeforeSave method, when the user is created the "else" part is run, when the used is edited the "if" part is run... and when the user is created the role field coming from the view is set, but that doesn't happen in the case of an edit, and that's the problem. – diminuta May 26 '14 at 09:43

0 Answers0