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

hierarchy from a database table to java object

我有一个table如下图: 'contract'

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

另一个table 'main contractor'

 company    main_contractor

  intel          x
  googl          m

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

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

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

如何使用 java 程序从 table 生成这样的分层数据?我在 SQL 中没有可用的特殊功能,例如 'connect by' 或 'with recursive',因为我使用的是 Ingres 数据库。

看起来很简单,我用一个名称和一组分包商为一家公司建模。覆盖 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);
}

您想要的最终输出只是打印列表 mainCompanies。