所以我有一个名为“用户”的实体
@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;
}
}
这里是UserProjectCenterRole实体:
@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;
}
}
在面板中,有一个下拉面板,其中用户的当前活动角色加载了“较低”的可能角色。选项是getRoles()的结果,所选的选项应该加载到“role”属性中。
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);
然后面板的onBeforeSave方法是:
@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);
}
但是在那时“getRole”返回null ......我无法猜到为什么......