Code:
try
{
session.save(((Object) p01TO));
insertCounter.incrementAndGet();
if (insertCounter.get() == batchSize) {
log.debug("p01DAO.insert() batch threshold hit. Flushing Hibernate session and clearing Hibernate session cache.");
insertCounter.set(0);
session.flush();
session.clear();
}
} catch (NonUniqueObjectException ex) {
log.debug("p01DAO.insert() batch threshold hit. Flushing Hibernate session and clearing Hibernate session cache after NonUniqueObjectException.");
insertCounter.set(0);
session.flush();
session.clear();
session.save(((Object) p01TO));
insertCounter.incrementAndGet();
if(insertCounter.get() == batchSize) {
log.debug("p01DAO.insert() batch threshold hit. Flushing Hibernate session and clearing Hibernate session cache.");
insertCounter.set(0);
session.flush();
session.clear();
}
In the above code , session.save was throwing NonUniqueObjectException exception so I caught that exception and then flushed and cleared the session and then saved the new object in the session for flushing it again.
But because of frequent flushing the performance has taken a hit . Will using named queries help me ??