英文:
How to iterate through table rows in Java?
问题
我想要通过JPA迭代访问SQL表格,以下是使用示例:
假设我有一个包含100列的"Person"表格:
在我的Java程序中,我想要执行以下操作(仅作为示例):
- 对于每一行数据
- 获取"firstname"字段
- 获取"number"字段
- 对"firstname"字段执行操作
- 对"number"字段执行操作
问题是,我不想创建一个"Person"的实体类,因为它包含了100列,显得很麻烦,而且我对其中的大部分字段都不感兴趣。
另外,我也不想以Object[]类型来检索行数据,因为假设我只想提取其中的40列,那么操作这样的数据类型会很混乱。
我正在使用Spring Data JPA而非JDBC。
英文:
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.
答案1
得分: 1
你可以创建一个仅包含你感兴趣列的实体类。
然后,使用 Spring 数据,你将获得一个 Person 类的集合,你可以按照你喜欢的方式进行迭代。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论