I have an object wich has a lazy-loading relationship . After I loaded the object (without loading the lazy-loading child), I closed the session, thus detaching the object.
If I understand correctly, if I try myObject.getLazyChild(), I will get a LazyInitializationException.
Is there a way to have the myObject.lazyChild to be automatically set to null in such a situation?
Of course, I can call have myObject.setLazyChild( null ); , but I’m looking for a way to do this automatically.
No, there’s no way. Why would you want to do this? If you try to access it and it has not been loaded, you’d better get an exception, informing you that you have a bug in your design, rather than
null, which might be a perfectly valid value for the field, and thus cause your applications to behave badly. Think about this use-case:If you forget to load the cancer field of the patient, you’ll let him go. I’d better get an exception, fix my code, and have the patient cured.