简体   繁体   English

JDBC一对多联接与多项选择

[英]JDBC One To Many Joins vs Multiple Selects

I am currently writing a program for which I need to select an entity from the database which has multiple one to many relationships. 我目前正在编写一个程序,需要从数据库中选择一个具有多个一对多关系的实体。 In this program the main object a Route is made of many Route Points has many Comments and many Reports. 在此程序中,由许多路径点组成的路径的主要对象具有许多注释和许多报告。 This means if i want to select a Route from the database along with its Route Points, Comments and Reports as a result of the join I am going to get multiple rows back with duplicate information in. 这意味着,如果由于联接而要从数据库中选择路由以及其路由点,注释和报告,我将获得多行并输入重复信息。

As far as I am aware this will mean I will have to loop on each row and keep a set of if I have already "seen" this RoutePoint, Report or Comment. 据我所知,这意味着我将不得不在每一行上循环,并保留一组是否已经“看到”此RoutePoint,报告或注释。 As a Route can have a large number of Route Points I imagine this would be quite slow as well as been quite complicated and messy. 由于一条路线可以具有大量的路线点,所以我想这会很慢,而且非常复杂和混乱。 I have included below what this would look like. 我将下面的内容包括在内。

HashSet<String> commentIds = new HashSet<String>();
HashSet<String> routePointIds = new HashSet<String>();
HashSet<String> reportIds = new HashSet<String>();

Route route = null;
        while (resultSet.next()) {
            String commentId = resultSet.getString("commentId");
            String routePointId = resultSet.getString("routePointId");
            String reportId = resultSet.getString("reportId");

            if(route == null){
                // create route from result set
            }

            if(!commentIds.contains(commentId)){
                // create comment object from result set
                // add to route object

                commentIds.add(commentId);
            }

            if(!routePointIds.contains(routePointId)){
                // create route point object from result set
                // add to route object

                routePointIds.add(routePointId);
            }

            if(!reportIds.contains(reportId)){
                // create comment object from result set
                // add to route

                reportIds.add(reportId);
            }

        }

My question is, is there a simpler way of processing a query with multiple one to many joins in JDBC? 我的问题是,在JDBC中是否有更简单的方法来处理具有多个一对多联接的查询?

Another way I have thought of approaching this is to do multiple selects. 我想到的另一种方法是进行多次选择。 One for each table rather than joining. 每个表一个,而不是联接一个。 I am not sure if this is a suitable way to do this as it would mean querying the database multiple times rather than just the once. 我不确定这是否是执行此操作的合适方法,因为这将意味着多次查询数据库,而不仅仅是一次查询。

Thanks for your replies. 多谢您的回覆。

Query for distinct rows for each independent result set. 查询每个独立结果集的不同行。 Since you are only querying for one route's data, I'm assuming you are not storing this in a cache or memory, and will need to perform this action any time you need the route data. 由于您仅查询一条路线的数据,因此我假设您没有将其存储在缓存或内存中,并且在需要路线数据时需要执行此操作。 It will more efficient to use an optimized query, and avoid java logic for each row returned to weed out duplicate data. 使用优化的查询将更有效,并且避免为返回的每一行使用Java逻辑以清除重复数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM