JPA: what is the proper pattern for iterating over large result sets?

后端 未结 15 2534
攒了一身酷
攒了一身酷 2020-11-27 09:50

Let\'s say I have a table with millions of rows. Using JPA, what\'s the proper way to iterate over a query against that table, such that I don\'t have all an in-memo

15条回答
  •  孤城傲影
    2020-11-27 10:35

    It depends upon the kind of operation you have to do. Why are you looping over a million of row? Are you updating something in batch mode? Are you going to display all records to a client? Are you computing some statistics upon the retrieved entities?

    If you are going to display a million records to the client, please reconsider your user interface. In this case, the appropriate solution is paginating your results and using setFirstResult() and setMaxResult().

    If you have launched an update of a large amount of records, you'll better keep the update simple and use Query.executeUpdate(). Optionally, you can execute the update in asynchronous mode using a Message-Driven Bean o a Work Manager.

    If you are computing some statistics upon the retrieved entities, you can take advantage on the grouping functions defined by the JPA specification.

    For any other case, please be more specific :)

提交回复
热议问题