christian wrote:
It's no different from any other exception, so handle it where you take care of RuntimeException, I guess.
There's an important difference in this case... unlike a NullPointerException, IndexOutOfBoundsException, or other JDK RuntimeException, the LazyInitializationException being thrown is outside the control of the data object calling it. Consider the following code:
Code:
public class Instructor {
private List coursesTaught = new List();
public boolean hasCourses {
return coursesTaught.size() > 0;
}
}
When I'm not running Hibernate, there might be Errors, but as far as I can tell, there are no RuntimeExceptions that can be thrown in this method.
When using Hibernate, there could be a LazyInitializationException. I could do as you suggest and modify my data model to catch Hibernate exceptions, but that would make my data model dependent on the Hibernate classes, ruin the 'transparency' of Hibernate, and require me to duplicate that code in every accessor method. I would like the capability to handle that exception somewhere in the size() method of the coursesTaught. There are many things I might do with the exception:
* Rethrow the Exception as some abstracted non-Hibernate exception that I can catch without creating a dependency on Hibernate
* Log the exception
* Load the information on demand through some other mechanism
* Substitute some default value
* Derive the data through some calculation