Results 1 to 2 of 2

Thread: JdbcTemplate and collections

  1. #1

    Default JdbcTemplate and collections

    Hi,

    Let's take as example the following SQL schema:
    -LINE_ITEM: LINE_ITEM_ID (INT, PK), LINE_DESC (VARCHAR(45))
    -ORDER: ORDER_ID (INT, PK), ORDER_DESC (VARCHAR(45))
    -ORDER_LINE_ITEM: LINE_ITEM_ID (INT, PK), ORDER_ID (INT)

    I'm trying to get an order from its ID using JdbcTemplate. Following the documentation, I need to use the queryForObject() method. I pass the SQL, the ID as an array of Object and the RowMapper implementation. But, in my case, the Order class has a List<LineItem> property, so it seems that I can't use queryForObject method directly. First, I need to query for the list of LineItem corresponding to the Order ID, so the List<LineItem> object is created. Then, I need to create the Order object, so I need to execute another query (also using the queryForObject method). At the end, I set the List<LineItem> property of the created Order object.

    To conclude, I need to execute two SQL statements. Is that the only solution or I'm missing something? I've checked in the chapter 13 of the documentation, but didn't find.

    Thanks for your help.
    Last edited by Miko95; Jan 29th, 2012 at 04:38 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Seems like a homework assignment (judging from the tables).. I suggest take a look at the API and classes provided. What you want is to execute a single query (not 1+N queries, I suggest google for that). You want to use a ResultSetExtractor and not a RowMapper.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •