Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:21:15+00:00 2026-06-15T06:21:15+00:00

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

  • 0

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

And let’s say that the my object Customer has two lists related to those two objects.

I’ve read this example:
http://forum.springsource.org/showthread.php?50617-rowmapper-with-one-to-many-query
which explains how to do it with a single one-to-many relationship. For your convenience here’s the ResultSetExtractor override:

private class MyObjectExtractor implements ResultSetExtractor{

    public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
        Map<Integer, MyObject> map = new HashMap<Integer, MyObject>();
        MyObject myObject = null;
        while (rs.next()) {
            Integer id = rs.getInt("ID);
            myObject = map.get(id);
          if(myObject == null){
              String description = rs,getString("Description");
              myObject = new MyObject(id, description);
              map.put(id, myObject);
          }
      MyFoo foo = new MyFoo(rs.getString("Foo"), rs.getString("Bar"));
      myObject.add(myFoo);
        }
        return new ArrayList<MyObject>(map.values());;
    }
}

I don’t think it covers how to work with both. What would be the cleanest approach? Is there a simpler way than to iterate with conditions? Would sets be better off than lists in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:21:17+00:00Added an answer on June 15, 2026 at 6:21 am

    From your question, I assume that you have three tables; Customer, Brands, Orders. If you want to fetch the Brands and Orders properties of the Customer to your customer object, where there is no relationship between Brands and Orders, what I suggest is to use a UNION query. Something like this:

    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
    

    Query:

    SELECT CUS.*, BRANDS.CUSTOMER_BRAND_ID COL_A, BRANDS.BRAND_NAME COL_B, 1 IS_BRAND FROM TBL_CUSTOMER CUS JOIN TBL_CUSTOMER_BRANDS BRANDS ON (CUS.CUSTOMER_ID = BRANDS.CUSTOMER_ID)
    UNION ALL
    SELECT CUS.*, ORDERS.ORDER_ID, '', 0 IS_BRAND FROM TBL_CUSTOMER CUS JOIN TBL_ORDERS ORDERS ON (CUS.CUSTOMER_ID = ORDERS.CUSTOMER_ID)
    

    Your ResultSetExtractor will become:

    private class MyObjectExtractor implements ResultSetExtractor{
    
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                Map<Long, Customer> map = new HashMap<Long, Customer>();
    
            while (rs.next()) {
                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);
                        }
    
                int type = rs.getInt("IS_BRAND");
                if(type == 1) {
                    List brandList = customer.getBrands();
                    if(brandsList == null) {
                        brandsList = new ArrayList<Brand>();
                        customer.setBrands(brandsList);
                    }
                    Brand brand = new Brand();
                    brand.setId(rs.getLong("COL_A"));
                    brand.setName(rs.getString("COL_B"));
                    brandsList.add(brand);
                } else if(type == 0) {
                    List ordersList = customer.getOrders();
                    if(ordersList == null) {
                        ordersList = new ArrayList<Order>();
                        customer.setOrders(ordersList);
                    }
                    Order order = new Order();
                    order.setId(rs.getLong("COL_A"));
                    ordersList.add(order);
                }
            }
            return new ArrayList<Customer>(map.values());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say that I have classes like this: public class Parent { public int
Let's say I'm creating an OpenGL game in C++ that will have many objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.