I'm using Hibernate 2.1.6 and I noticed that a few of the Exception classes (specifically JDBCException.java, StaleObjectStateException.java, and LazyInitializationException.java) log error messages in their constructors. I believe this is a flawed design. It should be up to the user of a framework to catch exceptions and log messages appropriately. Every app has different requirements for logging so it's dangerous to make assumptions in the exception classes about how things should be logged. This is especially true in a general purpose framework such as Hibernate. If the exception class handles logging, how is it supposed to know about the cases where the exception is caught and recovered from?
To make this issues more concrete:
I have a few places in my app where I don't know if a lazy collection is initialized or not, so I just try to retrieve it. If a LazyInitializationException occurs, I catach it and reattach the object to a new session with Session.update(). This works great, except my log is filled with bogus error messages due to the LazyInitializationException constructor. Sure, I can modify my log4j.properties to deal with this, but I don't think that should be necessary.
So I guess I'm making an enhancement request for Hibernate 3... could we get rid of these log messages in the Exception constructors?
Here's one of the relevant constructors:
Code:
public LazyInitializationException(String msg) {
super(msg);
LogFactory.getLog(LazyInitializationException.class).error(msg, this);
}
-Adam