multiple one-to-many relations ResultSetExtractor

后端 未结 4 2025
余生分开走
余生分开走 2020-12-08 05:46

Let\'s say I have an object with two different one-to-many relations. Much like:

Customer 1<->M Brands and Customer 1<->M Orders

4条回答
  •  没有蜡笔的小新
    2020-12-08 06:34

    I assume the model described by James Jithin in his answer:

    TBL_CUSTOMER
    ------------
    CUSTOMER_ID
    CUSTOMER_ACCOUNT_NO
    CUSTOMER_NAME
    
    TBL_CUSTOMER_BRANDS
    -------------------
    CUSTOMER_BRAND_ID            - UK
    BRAND_NAME
    CUSTOMER_ID                  - FK
    
    TBL_ORDERS
    -------------------
    ORDER_ID                     - UK
    CUSTOMER_ID                  - FK
    

    Instead of going for one Query, I would suggest the following three:

    SELECT CUS.* FROM TBL_CUSTOMER CUS 
    
    SELECT BRANDS.CUSTOMER_ID, BRANDS.CUSTOMER_BRAND_ID, BRANDS.BRAND_NAME FROM TBL_CUSTOMER_BRANDS BRANDS
    
    SELECT ORDERS.CUSTOMER_ID, ORDERS.ORDER_ID FROM TBL_ORDERS ORDERS 
    

    Your RowCallbackHandlers would become:

    private class CustomerRowCallbackHandler  implements RowCallbackHandler {
    
        private final Map customerMap;
    
        public BrandRowCallbackHandler(Map customerMap) { this.customerMap = customerMap}
    
        public void processRow(ResultSet rs) throws SQLException {
                Long id = rs.getLong("CUSTOMER_ID");
                Customer customer = map.get(id);
                if(customer == null){
                    customer = new Customer();
                    customer.setId(id);
                    customer.setName(rs.getString("CUSTOMER_NAME"));
                    customer.setAccountNumber(rs.getLong("CUSTOMER_ACCOUNT_NO"));
                    map.put(id, customer);
                        }
        }
    }
    
    private class BrandRowCallbackHandler implements RowCallbackHandler {
    
        private final Map customerMap;
    
        public BrandRowCallbackHandler(Map customerMap) { this.customerMap = customerMap}
    
        public void processRow(ResultSet rs) throws SQLException {
                Long id = rs.getLong("CUSTOMER_ID");
                Customer customer = map.get(id);
                if(customer != null){
                    List brandList = customer.getBrands();
                    if(brandsList == null) {
                        brandsList = new ArrayList();
                        customer.setBrands(brandsList);
                    }
                    Brand brand = new Brand();
                    brand.setId(rs.getLong("CUSTOMER_BRAND_ID"));
                    brand.setName(rs.getString("CUSTOMER_BRAND_NAME"));
                    brandsList.add(brand);
                } 
        }
    }
    
    private class OrderRowCallbackHandler implements RowCallbackHandler {
    
        private final Map customerMap;
    
        public OrderRowCallbackHandler(Map customerMap) { this.customerMap = customerMap}
    
        public void processRow(ResultSet rs) throws SQLException {
                Long id = rs.getLong("CUSTOMER_ID");
                Customer customer = map.get(id);
                if(customer != null){
                    List ordersList = customer.getOrders();
                    if(ordersList == null) {
                        ordersList = new ArrayList();
                        customer.setOrders(ordersList);
                    }
                    Order order = new Order();
                    order.setId(rs.getLong("ORDER_ID"));
                    ordersList.add(order);
                }
        }
    }
    

提交回复
热议问题