I am migrating a Swing desktop application with JDBC MySQL database access to JPA/Hibernate.
I wonder, if there is any built-in support for progress monitors or any recommended strategy for using progress monitors in JPA/Hibernate?
In JDBC I used this code fragment:
Code:
ResultSet rs;
PreparedStatement stmt = con.prepareStatement("SELECT ...",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery();
int rowCount = rs.getRow();
getProgressMonitor().setMaximum(rowCount);
rs.beforeFirst();
while (rs.next()) {
getProgressMonitor().setProgress(rs.getRow());
[... process row ...]
}
I searched for a similar functionality in JPA 2, but without success.
In Hibernate it is possible to use a scrollable query interface like in this fragment:
Code:
Query q = em.createNamedQuery("item.findAll");
QueryImpl qx=(QueryImpl) q;
// Scroll only works with normal queries, not with join fetch
qx.getHibernateQuery().scroll(ScrollMode.FORWARD_ONLY);
Iterator it=qx.getHibernateQuery().iterate();
getProgressMonitor().setMaximum(/* ??? */);
int counter;
Item item;
for(counter=0, item=null; it.hasNext(); counter++, item=it.next()) {
getProgressMonitor().setProgress(counter);
[... process item ...]
}
My questions: Is there a better interface for progress monitors? Is there an interface for inserts and updates?
A simple counter, which sets the progress for any processed object, which is saved or updated, may not be adequate, because the SQL execution may be delayed until flushing or commit.
Björn