从结果集中填充 bean 对象

发布于 2024-08-24 15:19:15 字数 624 浏览 10 评论 0原文

我面临着从结果集中填充 bean 对象的问题。

描述:Resultset 包含存储过程的结果,该存储过程是 3 个表 BBOBOV 的连接。

我有 3 个 POJO 对应于表。表之间的关系是:B可以有0个或多个BOBO可以有0个或多个BOV 的。因此,在结果集中我总共有 162 条记录,其中包含 B 的重复项。

例如:

B  BO   BOV
1  1     1
1  1     2
1  2     1
2  1     1 

等等。

实际上有 10 个不同的 B。所以我只想要结果集中的 10 个 B,而不是 162 条记录。另外,II 应该能够获得相应的 BOBOV,例如 B=1 BO 的所有值和 BOV 的。

我该怎么做?这是纯java逻辑,不能改变存储过程的任何内容。只需处理结果集即可。

I am facing a problem regarding populating bean objects from the resultset.

Description:Resultset contains the result of a stored procedure which is join of 3 tables B, BO, and BOV.

I have 3 POJO's corresponding to tables. The relation between tables is: B can have 0 or more BO's, BO can have 0 or more BOV's. So totally in resultset I have 162 records, which contain duplicates for B.

For example:

B  BO   BOV
1  1     1
1  1     2
1  2     1
2  1     1 

and so on.

Actually there are 10 distinct B's. So II want only 10 B's from the resultset not 162 records. Also II should be able to get corresponding BO and BOV's like for B=1 all values of BO and BOV's.

How can I do this? This is pure java logic and cannot change anything for the stored procedure. Just have to process the resultset.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶花眉 2024-08-31 15:19:15

这里有一个正在运行的结果集 Map 是一个可能的伪代码

[仅伪代码...不保证编译]

Map mapofBs = new HashMap();
while(rs.hashNext()) {
    rs.next();
    String bId = rs.getString("columnname for id of b");
    B objectB = mapofBs.get(bId);
    if(objectB == null) {
        objectB = new B();
        //read relevant columns from result set and put into objectB
        mapOfBs.put(bId, objectB)
    }   
    //now onto the boId
    String boId = rs.getString("columnname for id of BO");
    BO objectBO = objectB.getBOForId(boId);
    if(objectBO == null) {
        objectBO = new BO();
        //read relevat columns from result set for objectBO
        objectB.addObjectBO(objectBO);
    }
    String bovID = s.getString("columnname for id of BOV");
    BOV objectBOV = objectBO.getBOVForId(bovId);
    if(objectBOV == null) {
        objectBOV = new BOV();
        //read relevat columns from result set for objectBOV
        objectBO.addObjectBOV(objectBOV);
    }
}
//mapOfBs.keySet() gives you a Set<B> which you are interested in

Have a running Map of the resultset here is a possible pseudocode

[pseudo code only... not guaranteed to compile]

Map mapofBs = new HashMap();
while(rs.hashNext()) {
    rs.next();
    String bId = rs.getString("columnname for id of b");
    B objectB = mapofBs.get(bId);
    if(objectB == null) {
        objectB = new B();
        //read relevant columns from result set and put into objectB
        mapOfBs.put(bId, objectB)
    }   
    //now onto the boId
    String boId = rs.getString("columnname for id of BO");
    BO objectBO = objectB.getBOForId(boId);
    if(objectBO == null) {
        objectBO = new BO();
        //read relevat columns from result set for objectBO
        objectB.addObjectBO(objectBO);
    }
    String bovID = s.getString("columnname for id of BOV");
    BOV objectBOV = objectBO.getBOVForId(bovId);
    if(objectBOV == null) {
        objectBOV = new BOV();
        //read relevat columns from result set for objectBOV
        objectBO.addObjectBOV(objectBOV);
    }
}
//mapOfBs.keySet() gives you a Set<B> which you are interested in
暖阳 2024-08-31 15:19:15

更改查询以包含 GROUP BY 是最好的选择。

Changing the query to include a GROUP BY is much the best option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文