简体   繁体   中英

How to iterate through table rows in Java?

I want to iterate through an sql table using JPA, example of use:
I have the table Person which contains 100 columns

id | gender | firstname | lastname |  address1 | address2 | number | ....+100
------------------------------------------------------------------------------
-  |  ...   |   .....   |   ...    |   ....    |   ....   |   ..   |  ....
------------------------------------------------------------------------------
-  |  ...   |   .....   |   ...    |   ....    |   ....   |   ..   |  ....

and in my Java program, I want to do the following (as an example):

  • for each row
  • get the firstname
  • get the number
  • do operation with firstname
  • do operation with number

the problem is that I don't want to create an Entity class of Person because it contains 100 columns and it's quite cumbersome and I'm not interested with all of them.

Also, I don't want to retrieve the row in an Object[] type, because suppose that I want to extract 40 columns out of 100, it will be confusing to manipulate such a type.

I'm using spring data JPA and not JDBC.

You can create an entity class with only the columns you're interrested in.

And then, with Spring data, you will get a collection of Person class which you can iterate through the way you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM