具有本机查询的JPA2.0 / Hibernate - 当返回多行时,getResultList()很慢

时间:2015-03-11 10:44:32

标签: hibernate jpa-2.0

我使用JPA2.0和Hibernate作为ORM。我正在尝试获取可能有数百万条记录的表数据。我编写了本机查询并尝试使用JPA2.0 / Hibernate获取它。

Query query = entityManager.createNativeQuery("Select empId, empName, empDepart from employee");
final List<Object[]> stList = (List<Object[]>) query.getResultList();
for (Object[] objects : stList) {
     // Fetching data and putting into my VOs here
}

当我启用了休眠日志记录时,会打印下面的日志。这里发生的是这个查询返回5000行,如果你看到下面的日志“结果行:”一个接一个地打印,直到所有5000行被提取。完成需要3-4分钟。有没有更快的方法来加载JPA / Hibernate中的所有5000条记录。有什么想法吗?

2015-03-11 15:51:01,850 DEBUG - begin
2015-03-11 15:51:01,850 DEBUG - opening JDBC connection
2015-03-11 15:51:04,026 DEBUG - current autocommit status: true
2015-03-11 15:51:04,026 DEBUG - disabling autocommit
2015-03-11 15:51:04,057 DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2015-03-11 15:51:04,059 DEBUG - Select empId, empName, empDepart from employee
2015-03-11 15:51:04,627 DEBUG - about to open ResultSet (open ResultSets: 0, globally: 0)
2015-03-11 15:51:04,632 DEBUG - result row: 
2015-03-10 19:14:11,614 DEBUG - result row: 
2015-03-10 19:14:11,614 DEBUG - result row: 
2015-03-10 19:14:11,615 DEBUG - result row: 
.
. 
.
2015-03-11 15:53:51,950 DEBUG - result row: 
2015-03-11 15:53:51,950 DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1)
2015-03-11 15:53:51,950 DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2015-03-11 15:53:51,951 DEBUG - initializing non-lazy collections
2015-03-11 15:53:51,952 DEBUG - rollback
2015-03-11 15:53:52,189 DEBUG - re-enabling autocommit
2015-03-11 15:53:52,189 DEBUG - rolled back JDBC Connection
2015-03-11 15:53:52,190 DEBUG - aggressively releasing JDBC connection
2015-03-11 15:53:52,190 DEBUG - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2015-03-11 15:53:52,445 INFO  - closing

0 个答案:

没有答案