开发者社区> 问答> 正文

添加顶级机构,自动添加一个空的父机构对象是什么原因

问题说明:机构管理的添加顶级机构,自动添加一个空的顶级机构的父机构对象, 而不是把顶级机构的父机构设置为空

@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

展开
收起
云栖技术 2016-06-02 15:29:44 2250 0
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!

    因为在数据库中id parent是主外键关系。实体中parent的类型为organization

    2019-07-17 19:25:01
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
对象的生命期管理 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多