問題說明:機構管理的添加頂級機構,自動添加一個空的頂級機構的父機構對象, 而不是把頂級機構的父機構設置為空
@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
37 | hjh | hgg | null_37 | 36
備注: 添加機構下面的子機構毫無問題
因為在數據庫中id parent是主外鍵關系。實體中parent的類型為organization