Hibernate version:3.0.5
Hi everybody!
I am using many-to-one association with lazy loading enabled (default option in Hibernate 3). As a result, all references to classes which are mapped trough this association are proxied, which is good.
What bad (at least for me) is,
when proxied object's method throws an exception, this exception will arrive as InvocationTargetException instead of original which I would expect.
For example consider the following model:
Code:
class A {
B b; //This one is mapped as many-to-one association
setB(B b) {
this.b = b;
}
getB() {
return b;
}
}
class B {
public void foo() throws MyException {
throw new MyException();
}
}
class MyException extends Exception {
}
When I'll call a.getB().foo() there will be 2 different behaviors:
1. If lazy loading is disabled, I'll get MyException.
2. If lazy loading is enabled, I'll get InvocationTargetException.
The second one is very dangerous. It bypasses type checking (MyException is NOT a RuntimeException) and causes unpredicted behavior.
CGLIBLazyInitializer could easily catch an InvocationTargetException, and rethrow it's cause.
What do you think? Or am I missing something?
Thanks a lot!
Michael.