1

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.

1 Answer 1

4

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 the patient's cancer field is null, then let him go
  • else, start curing him immediately

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.