0
我目前正在编写一个程序,我需要从数据库中选择一个具有多个一对多关系的实体。在这个程序中,主要对象Route由多个Route Points组成,有许多评论和许多报告。这意味着如果我想从数据库中选择一条路由以及它的路由点,评论和报告作为连接的结果,我将获取多行返回的重复信息。JDBC一对多连接与多选择
据我所知这意味着我将不得不在每一行上循环,并保留一组如果我已经“看到”这个路由点,报告或评论。由于路线可以有大量的路线点,我想这会很慢,而且相当复杂和混乱。我在下面包括了这个看起来像什么。
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);
}
}
我的问题是,是否有一种更简单的方法来处理JDBC中多个一对多连接的查询?
另一种解决这个问题的方法是做多个选择。每个桌子一个而不是加入。我不确定这是否是一种合适的方式来执行此操作,因为这意味着多次查询数据库而不是仅查询一次。
感谢您的回复。
类似的要求在这里讨论http://stackoverflow.com/questions/13295600/multiple-one-to-many-relations-resultsetextractor –