Transient wicket form field ignored

≡放荡痞女 提交于 2019-12-11 18:48:25

问题


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...

来源:https://stackoverflow.com/questions/23865997/transient-wicket-form-field-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!