0

This is what the rs gets as a result:

Order(id=1, orderNumber=A1, orderRows=[OrderRow(itemName=Motherboard, quantity=0, price=5), OrderRow(itemName=Cpu, quantity=5, price=0)])

And now I am trying to get both of the OrderRows:

private RowMapper<Order> getPostRowMapper() {
    return (rs, rowNum) -> {
        Order order = new Order();
        order.setId(rs.getLong("id"));
        order.setOrderNumber(rs.getString("ordernumber"));
        System.out.println("quan" + rs.getString("quantity")); // Prints 0
        System.out.println("quan" + rs.getString("price")); // Prints 5
        System.out.println("quan" + rs.getString("quantity")); // Prints 0 (should print 5)

        return order;
    };
}

This is how I would do it if i could do it in a loop if I could seperate the OrderRows somehow:

//For the first iteration
if(resultSet.getString("itemname") != null){
    order.add(new OrderRow(resultSet.getString("itemname"),
            resultSet.getInt("quantity"), resultSet.getInt("price")));
}
while (resultSet.next()){
    if(resultSet.getString("itemname") != null){
        order.add(new OrderRow(resultSet.getString("itemname"),
                resultSet.getInt("quantity"), resultSet.getInt("price")));
    }
}

I have two Objects one is Order and the other one is OrderRow. Is it possible to create multiple objects in one RowMapper, both Order/OrderRow or do I need multiple requests for that?

Or can I somehow go over all of the OrderRows that are in the result set and add make them into OrderRow objects in the iteration?

Kolka Tankari
  • 49
  • 1
  • 6
  • Think about how to do this using a `for loop` – Scary Wombat Nov 13 '18 at 02:02
  • Well thats what I am asking about how can I put it in a `for loop` to go over all of the `OrderRows`. `hasNext` does not work here and after that I am not quite sure how to do it. – Kolka Tankari Nov 13 '18 at 02:04
  • show us the code that uses a for loop – Scary Wombat Nov 13 '18 at 02:07
  • I am sorry but I do not get what do you mean. I do not have a working code that would work with `for loop`, which is the reason I am asking – Kolka Tankari Nov 13 '18 at 02:10
  • Can you try writing the code and then we can show you maybe where you are going wrong. Not going to write the code for you. – Scary Wombat Nov 13 '18 at 02:12
  • Well I edited as to how I would do it but I really have no idea how I should get the `OrderRow` objects looping. – Kolka Tankari Nov 13 '18 at 02:23
  • Looking at (and trying to understand) it appears to me the you have `one` rows returned - within that `row` you have an array, so you can use `rs.getString("ordernumber"` then `resultset.getArray("orderRows");` then you can iterate over this `Array` – Scary Wombat Nov 13 '18 at 02:30
  • I have tried getting the array that way but it throws me an error that it cannot find it. Here is more detailed question: [link to question](https://stackoverflow.com/questions/53271486/creating-multiple-object-with-one-response-using-spring-framework) . I have been at it for quite some time now and tried many solutions but none work. – Kolka Tankari Nov 13 '18 at 02:35
  • How about actually posting your error and the code you are using. Also, how did you ascertain that your `rs` is `Order(id=1, orderNumber=A1, orderRows=[OrderRow(itemName=Motherboard, quantity=0, price=5), OrderRow(itemName=Cpu, quantity=5, price=0)])` Give clear concise information – Scary Wombat Nov 13 '18 at 02:40
  • [Please add a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Scary Wombat Nov 13 '18 at 02:40
  • Are you posting as multiple users? – Scary Wombat Nov 13 '18 at 02:40
  • why have you abandoned this question? That's not very polite. – Scary Wombat Nov 14 '18 at 02:06

0 Answers0