When all of the values in an @Embedded object are NULL, Hibernate sets the field in the parent object to null. This can lead to NullPointerExceptions if not handled correctly.
There are two ways to handle this:
- Make sure code that calls the @Embedded getter handles null return values.
- Add a non-nullable field to the embedded object, thus avoiding the behavior entirely.
I suggest to make this optional. I.e. add an annotation which would make Hibernate create an object with it's properties set to null.
In my oppinion, this would improve usability and user friendliness.
Having an embedded object with all props potentially set to null is perfectly valid use case - e.g. an object with optional traits grouped in @Embeddable class; When this is created, the traits are initially empty.
This was reported years ago as a bug, but at that time, it was closed as Won't fix for unknown reason.