Hi Hibernate Users,
i've a question regarding a query execution which is located within a loop. Imagine following scenario.
Code:
List<SomeObject> allSo = new ArrayList<SomeObject>();
while(resultSet.next()){
Long someColumnValue = result.getLong("COLUMN_NAME");
SomeObject so = (SomeObject) em.createNamedQuery("getSomeObjectBySomeColumn").setParameter("someColumn",someColumnValue).getSingleResult();
allSo.add(so);
}
Within this loop the query execution time slows down. The first time it might be a millisecond but after 100 iteration steps it will be 100 ms per execution and rising. I've profiled the application and saw that the method autoFlushIfRequired has a very high cost.
I know that hibernate wants to flush the session before every query execution and it check whether a flush is needed or not (autoFlushIfRequired). But this is unuseable. Might this be a bug? Is it already posted? I couild not find it!
I've tried to flush and clear the entitymanger which will increase the performance, but I don't want to do this, because it will clear other objects too which I need later on again (It's a long transaction).
What can I do to execute a query within a loop which won't last longer every iteration step?
Thx for suggestions