Multiple objects from one ResultSet using JDBC Template with Spring











up vote
0
down vote

favorite
1












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?










share|improve this question
























  • Think about how to do this using a for loop
    – Scary Wombat
    Nov 13 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 at 2:04










  • show us the code that uses a for loop
    – Scary Wombat
    Nov 13 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 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 at 2:12















up vote
0
down vote

favorite
1












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?










share|improve this question
























  • Think about how to do this using a for loop
    – Scary Wombat
    Nov 13 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 at 2:04










  • show us the code that uses a for loop
    – Scary Wombat
    Nov 13 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 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 at 2:12













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 2:22

























asked Nov 13 at 1:52









Kolka Tankari

175




175












  • Think about how to do this using a for loop
    – Scary Wombat
    Nov 13 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 at 2:04










  • show us the code that uses a for loop
    – Scary Wombat
    Nov 13 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 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 at 2:12


















  • Think about how to do this using a for loop
    – Scary Wombat
    Nov 13 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 at 2:04










  • show us the code that uses a for loop
    – Scary Wombat
    Nov 13 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 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 at 2:12
















Think about how to do this using a for loop
– Scary Wombat
Nov 13 at 2:02




Think about how to do this using a for loop
– Scary Wombat
Nov 13 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 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 at 2:04












show us the code that uses a for loop
– Scary Wombat
Nov 13 at 2:07




show us the code that uses a for loop
– Scary Wombat
Nov 13 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 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 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 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 at 2:12

















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',
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
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents