Multiple objects from one ResultSet using JDBC Template with Spring
up vote
0
down vote
favorite
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
up vote
0
down vote
favorite
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 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 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 withfor 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
|
show 7 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 2:22
asked Nov 13 at 1:52
Kolka Tankari
175
175
Think about how to do this using afor loop
– Scary Wombat
Nov 13 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 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 withfor 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
|
show 7 more comments
Think about how to do this using afor loop
– Scary Wombat
Nov 13 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 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 withfor 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
|
show 7 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 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 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