问题描述
- 添加顶级机构,自动添加一个空的父机构对象是什么原因
-
问题说明:机构管理的添加顶级机构,自动添加一个空的顶级机构的父机构对象, 而不是把顶级机构的父机构设置为空@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