I\'m using Hibernate EntityManager, and am experiencing a weird slowdown in my Hibernate queries. Take a look at this code:
public void testQuerySpeed() {
I had essentially the same problem (query inside a loop). I proceeded a profiling verification with JProfiler...the execution of my interested method spent 572 seconds and hibernate dirty-checking takes 457 seconds of this time (about 80%). Amazing, isn't it? I must say that I had a lot of entities managed by EntityManager. If I introduce em.flush()/em.clear() or em.setFlushMode(FlushModeType.COMMIT) in the offending code, the performance problem goes away.
profiler result is available at http://img1.imagilive.com/0110/hibernate_dirty_checking_bad_perfomances0ce.png