问题说明:机构管理的添加顶级机构,自动添加一个空的顶级机构的父机构对象, 而不是把顶级机构的父机构设置为空
@Entity
@Table(name="t_organization")
public class Organization {
private int id;
private String name;
private String sn;
private String description;
private Organization parent;
private Set<Organization> children = new HashSet<Organization>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne(cascade=CascadeType.ALL)
public Organization getParent() {
return parent;
}
public void setParent(Organization parent) {
this.parent = parent;
}
@OneToMany(mappedBy="parent",cascade=CascadeType.ALL)
public Set<Organization> getChildren() {
return children;
}
public void setChildren(Set<Organization> children) {
this.children = children;
}
}
@Component("org")
@Scope(value = "propertype")
public class OrgAction implements ModelDriven{
//extends ActionSupport {
private Organization organization;
private OrgManager orgManager;
private int ppid;
@Override
public Organization getModel() {
if(organization == null ) {
organization = new Organization();
}
return organization;
}
public int getPpid() {
return ppid;
}
public void setPpid(int ppid) {
this.ppid = ppid;
}
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
@Resource(name = "orgManager")
public void setOrgManager(OrgManager orgManager) {
this.orgManager = orgManager;
}
public String orgList() {
ActionContext.getContext().put("orgs",
orgManager.searchOrganizations(this.organization == null ? 0 :
(this.organization.getParent() == null ? 0 : this.organization.getParent().getId()))
);
if(this.organization != null) {
if(this.organization.getParent() != null) {
Organization parent = this.orgManager.findOrganizationById(this.organization.getParent().getId());
if(parent.getParent() != null){
ppid = parent.getParent().getId();
}
}
}
/*for (Organization org : orgs) {
System.out.println(org.getName());
} */
return "org_list";
}
public String addInput() {
return "add_input";
}
public String add() {
orgManager.addOrg(organization, organization.getParent().getId());
return "add";
}
public String del() {
this.orgManager.delOrg(this.organization.getId());
return "del_success";
}
}
数据库显示:
26 | NULL | NULL | NULL | NULL
27 | s | s | null_27 | 26
28 | NULL | NULL | NULL | NULL
29 | v | v | null_29 | 28
30 | NULL | NULL | NULL | NULL
31 | m | m | null_31 | 30
32 | NULL | NULL | NULL | NULL
33 | v | v | null_33 | 32
34 | NULL | NULL | NULL | NULL
35 | i | i | null_35 | 34
36 | NULL | NULL | NULL | NULL
因为在数据库中id parent是主外键关系。实体中parent的类型为organization
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。