1

I am trying to save objects in a loop, and I don't want to stop my work when it come across errors, so my code looks like follows:

for(Model model:list){
 try {

                if (model != null) {
                    getHibernateTemplate().saveOrUpdate(model);
                    getHibernateTemplate().flush();
                } 
            } catch (Exception e) {
               log.error(e);
                if (model!= null) {
                    getHibernateTemplate().getSessionFactory().evict(Model.class, model.getId());
                }
                getHibernateTemplate().evict(model);
            }
}

It works fine except when one object saving failed, all the rest object failed with java.lang.NullPointerException. Absolutely the Hibernate session is not null according to my debug tracing. Is there anything wrong with my code? Any comments would be great appreciated!

3
  • try to put this code ` getHibernateTemplate().flush();` out of if statement Commented May 28, 2013 at 8:39
  • HibernateTemplate is quite old. Its usage is highly discouraged for a long time. Hibernate 4.x doesn't support it anymore. Why do you still use it? Commented May 28, 2013 at 8:40
  • @Lion it is a legacy system, but problem still exists even I replace getHibernateTemplate() with session. Commented May 28, 2013 at 8:45

1 Answer 1

1

When the session throws an exception, you should not continue with it because it is in an inconsistent state. You should do the saves inside a transaction on rollback everything if an exception is thrown.

Search for "don't flush the Session after an exception occurs" for more info

Also, this part of your code looks weird:

if (model!= null) {
   getHibernateTemplate().getSessionFactory().evict(Model.class, model.getId());
}
getHibernateTemplate().evict(model);

What it is supposed to do when model is null? Shouldn't you put the last line inside the if statement?

Sign up to request clarification or add additional context in comments.

6 Comments

yes, I know that it is not appropriate to do that when an exception is thrown, but you see I have evicted the failed object from cache of session thus it should works correctly with the rest objects.
The doc doesn't say that evicting the failed object repairs the session, and as you've seen it doesn't work.
I see...thanks @Guillaume , so what do you think is the best approach to handle this situation if I want to continue the persistence work within a same transaction?
What exception are you trying to deal with in the first place? You need to perform some checks before trying to save the object. Another way would be to start a new session and transaction for every object you're saving.
Actually I have no idea of what exactly kind of exception could be thrown during the save, anyway I got your point that maybe perform some checks could avoid most of the failures.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.