从数据库表到java对象的层次结构

时间:2015-09-28 09:00:20

标签: java sql database tree hierarchy

我有一张桌子如下图所示: '合同'

company    contractor  subcontractor

  intel         x            y
  intel         x            z   
  intel         x            a
  intel         y            a
  intel         y            b
  intel         p            q
  intel         p            r
  intel         q            s
  googl         m            n
  googl         n            r
  googl         n            f
另一张桌子 '主要承包商'

 company    main_contractor

  intel          x
  googl          m

我需要使用java程序将此表中的数据提取到java对象或json作为层次结构。

[
 [Intel, [x, [y, [a,b],z,a]],

       [p, [q, [s],r]],
 ],
 [googl, [m, [n, [[r,f]]
         ]
  ]
]

如何使用java程序从表中生成这样的分层数据?我没有'具有SQL中可用的特殊功能,例如' connect by'或者'递归'因为我使用的是Ingres db。

1 个答案:

答案 0 :(得分:0)

看起来相当直接,我模仿了一家名称和一组分包商的公司。覆盖hashCode和equals所以相等只取决于在hashSet中使用的名称。

static class Company {

    public String getName() {
        return name;
    }

    public Set<Company> getSubcontractors() {
        return subcontractors;
    }
    final String name;
    final Set<Company> subcontractors = new HashSet<>();
    public Company(String name) {
        this.name = name;
    }        

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Company other = (Company) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Company{" + "name=" + name + ", subcontractors=" + subcontractors + '}';
    }
}

使用以下方法创建所有公司对象:

Connection con = DriverManager.getConnection(" ... ");
PreparedStatement stmt = con.prepareStatement("Select * from main_contractor");
ResultSet rs = stmt.executeQuery();
Map<String,Company> map = new HashMap<>();
List<Company> mainCompanies = new ArrayList<>();
while (rs.next()) {
    String companyName = rs.getString("company");
    Company company = new Company(companyName);
    map.put(companyName, company);
    mainCompanies.add(company);
    String mainContractor = rs.getString("main_contractor");
    Company contractor = new Company(mainContractor);
    company.getSubcontractors().add(contractor);
    map.put(mainContractor, contractor);
}
PreparedStatement stmt2 = con.prepareStatement("Select * from contract");
rs = stmt2.executeQuery();
while (rs.next()) {
    String contractorName = rs.getString("contractor");
    Company contractor = map.containsKey(contractorName)? map.get(contractorName): new Company(contractorName);
    map.put(contractorName, contractor);
    String subContractorName = rs.getString("subcontractor");
    Company subcontractor = map.containsKey(subContractorName)? map.get(subContractorName): new Company(subContractorName);
    contractor.getSubcontractors().add(subcontractor);
    map.put(subContractorName, subcontractor);
}

您想要的最终输出只是打印主要公司列表。