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 7582349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:52+00:00 2026-05-30T18:24:52+00:00

For a new project, we’ve decided to use Spring MVC, and JdbcTemplate (specifically SimpleJdbcTemplate)

  • 0

For a new project, we’ve decided to use Spring MVC, and JdbcTemplate (specifically SimpleJdbcTemplate) for persisting domain objects. One issue I’ve been having with this approach is how to cleanly create object graphs from SELECT queries. The RowMapper mechanism seems to work great when I am pulling rows from a single table; I become concerned when I’m mapping the results of a JOIN query.

To give a concrete (yet entirely fabricated) example, suppose I have two entities in an N-to-1 relationship:

public class Invoice {
    private Customer customer;
    ...
}

public class Customer {
    private int id;
    private String name;
    ...
}

I would like to be able to call a selectInvoices() method on my InvoiceDAO, and retrieve a list of Invoice‘s populated with fully-formed Customer instances. Instead, I find myself tempted to do something like the following:

public class Invoice {
    // this makes me unhappy
    private int customerId;
    ...
}

What is the best practice for cleanly making this happen? Should I just bite the bullet and use an ORM?

  • 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-05-30T18:24:54+00:00Added an answer on May 30, 2026 at 6:24 pm

    This is exactly what an ORM is good at. If you want to do it all by yourself, my trick is to use a map to hold the customers:

    Map<Long, Customer> customersById = new HashMap<Long, Customer>();
    ...
    public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException {
        Invoice invoice = ...
        // populate invoice fields
        Long customerId = rs.getLong("customerId");
        Customer c = customersById.get(customerId);
        if (c == null) {
            // first time we meet this customer
            c = ...
            // populate customer fields from result set
            customersById.put(customerId, c);
        }
        invoice.setCustomer(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting a new project and would like to use one of the MVC framworks.
I'm starting a new project with spring mvc 3 and spring security 3.1.0. I
In a new project, we decide to use Gooogle Web Font. It pretty easy
I'm starting a new project and I want to use NHibernate 3.2. I know
I created a new project of type Asp.net MVC 4 I installed the package
I'm starting a new project shortly and going to use coding standards. I've always
While recently imported a new project into eclipse, at one point when I tried
I'm starting one new project, and I need to choose the best technology to
We are starting a new project based on EJB 3.0. I have a spring
I can't create New Project on my Visual Web Developer 2008 Express with SP1.

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.