Multiple objects from one ResultSet using JDBC Template with Spring
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?
java sql spring resultset spring-jdbc
|
show 7 more comments
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?
java sql spring resultset spring-jdbc
Think about how to do this using afor loop
– Scary Wombat
Nov 13 '18 at 2:02
Well thats what I am asking about how can I put it in afor loop
to go over all of theOrderRows
.hasNext
does not work here and after that I am not quite sure how to do it.
– Kolka Tankari
Nov 13 '18 at 2:04
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2:07
I am sorry but I do not get what do you mean. I do not have a working code that would work withfor loop
, which is the reason I am asking
– Kolka Tankari
Nov 13 '18 at 2: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 2:12
|
show 7 more comments
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?
java sql spring resultset spring-jdbc
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?
java sql spring resultset spring-jdbc
java sql spring resultset spring-jdbc
edited Nov 13 '18 at 2:22
Kolka Tankari
asked Nov 13 '18 at 1:52
Kolka TankariKolka Tankari
225
225
Think about how to do this using afor loop
– Scary Wombat
Nov 13 '18 at 2:02
Well thats what I am asking about how can I put it in afor loop
to go over all of theOrderRows
.hasNext
does not work here and after that I am not quite sure how to do it.
– Kolka Tankari
Nov 13 '18 at 2:04
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2:07
I am sorry but I do not get what do you mean. I do not have a working code that would work withfor loop
, which is the reason I am asking
– Kolka Tankari
Nov 13 '18 at 2: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 2:12
|
show 7 more comments
Think about how to do this using afor loop
– Scary Wombat
Nov 13 '18 at 2:02
Well thats what I am asking about how can I put it in afor loop
to go over all of theOrderRows
.hasNext
does not work here and after that I am not quite sure how to do it.
– Kolka Tankari
Nov 13 '18 at 2:04
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2:07
I am sorry but I do not get what do you mean. I do not have a working code that would work withfor loop
, which is the reason I am asking
– Kolka Tankari
Nov 13 '18 at 2: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 2:12
Think about how to do this using a
for loop
– Scary Wombat
Nov 13 '18 at 2:02
Think about how to do this using a
for loop
– Scary Wombat
Nov 13 '18 at 2: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 2:04
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 2:04
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2:07
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2: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 2:10
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 2: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 2:12
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 2:12
|
show 7 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272649%2fmultiple-objects-from-one-resultset-using-jdbc-template-with-spring%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272649%2fmultiple-objects-from-one-resultset-using-jdbc-template-with-spring%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Think about how to do this using a
for loop
– Scary Wombat
Nov 13 '18 at 2:02
Well thats what I am asking about how can I put it in a
for loop
to go over all of theOrderRows
.hasNext
does not work here and after that I am not quite sure how to do it.– Kolka Tankari
Nov 13 '18 at 2:04
show us the code that uses a for loop
– Scary Wombat
Nov 13 '18 at 2: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 2: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 2:12