Hibernate/JPA newbie here, and I have a question on performance when loading a large number of rows - about 200,000 in my test case. I'm comparing performance vs. straight JDBC and have turned off JPA caching. My test with Hibernate 6.1 took about 6.1 seconds to load 160k rows from a single table with no joins, while the JDBC version took about 3.2 seconds. Looking for opinions and comments as to why it is so comparatively slow. Here is the relevant code:
JPA version: public List<FieldSecurity> loadFieldSecurity() { List<FieldSecurity> list = null; EntityManager em = getEntityManagerFactory().createEntityManager(); StringBuilder sql = new StringBuilder(); sql.append("select FieldSecurity.* from FieldSecurity "); sql.append("where FieldSecurity.userId is null OR FieldSecurity.userId=0"); list = (List<FieldSecurity>)em.createNativeQuery(sql.toString(), FieldSecurity.class) .getResultList(); em.close(); return list; }
JDBC version: public List<FieldSecurity> loadFieldSecurity() { List<FieldSecurity> list = null; StringBuilder sql = new StringBuilder(); sql.append("select FieldSecurity.* from FieldSecurity "); sql.append("where FieldSecurity.userId is null OR FieldSecurity.userId=0"); try { Class.forName("com.ibm.as400.access.AS400JDBCDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:as400://system/library","user","password"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql.toString()); list = new LinkedList<FieldSecurity>(); while (rs.next()) { FieldSecurity fs = new FieldSecurity(); FieldSecurityKey key = new FieldSecurityKey(); key.setApplicationId(rs.getLong("applicationId")); key.setRoleId(rs.getLong("roleId")); key.setFieldId(rs.getLong("fieldId")); key.setUserId(rs.getLong("userId")); fs.setKey(key); fs.setAccessType(rs.getString("accessType").charAt(0)); list.add(fs); } rs.close(); stmt.close(); conn.close(); } catch (Throwable t) { t.printStackTrace(); } return list; }
|