0

When we do a

session.save()

in hiberbate side it might throw different exceptions.Do we need to handle them.Is so how we should do it?

I found two implementations of save() (In SessionImpl and SessionDelegatorBaseImpl) and in the save() of SessionImpl it throws hibernate exceptions.

Can anyone please explain these stuff to me?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50

3 Answers3

1

Most of the Hibernate exceptions are RuntimeExceptions so that you should handle then only if you want to.

Therefore, starting with Hibernate 3.x, all exceptions thrown by Hibernate are subtypes of the unchecked Runtime Exception, which is usually handled in a single location in an application. This also makes any Hibernate template or wrapper API obsolete.

This might help why hibernate changed HibernateException to (unchecked) RuntimeException

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Whether an exception is checked or not must never influence the decision where to handle it. It is not up to the API to decide that for you. That is precisely why checked exceptions are Java's misfeature. – Marko Topolnik Apr 29 '13 at 09:25
  • @MarkoTopolnik: Yes, completely agree. Looking at OPs question and comments I thought he is confused about which type of exceptions are thrown and do he need to handle them. So I answered accordingly. – Ajinkya Apr 29 '13 at 10:02
0

You can catch the exceptions and throw another more meaningful exception and catch it in the appropriate layer where you can adequately handle the exception.

MMeersseman
  • 2,541
  • 1
  • 13
  • 8
  • I will have to know what exceptions will be thrown when saving in order to handle them or should I catch the larger Exception and handle all of them in same way? – Sanjaya Liyanage Apr 29 '13 at 08:48
0

If you caught an exception while saving, you have to think about rolling back, depending on your application needs of course.

Eugene
  • 117,005
  • 15
  • 201
  • 306