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

后端 未结 15 2581
攒了一身酷
攒了一身酷 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

    To expand on @Tomasz Nurkiewicz's answer. You have access to the DataSource which in turn can provide you with a connection

    @Resource(name = "myDataSource",
        lookup = "java:comp/DefaultDataSource")
    private DataSource myDataSource;
    

    In your code you have

    try (Connection connection = myDataSource.getConnection()) {
        // raw jdbc operations
    }
    

    This will allow you to bypass JPA for some specific large batch operations like import/export, however you still have access to the entity manager for other JPA operations if you need it.

提交回复
热议问题