How can I prevent JPA from setting an embeddable object to null just because all its fields are null?

前端 未结 2 1766
再見小時候
再見小時候 2021-02-14 08:00

Although counterintuitive and apparently not required by the JPA standard, both Eclipselink and Hibernate go to great lengths to create the following source of NullPointerExcept

2条回答
  •  误落风尘
    2021-02-14 08:30

    The JPA spec totally ignores handling of null embedded objects and leaves it up to implementations to do what they feel like (nice, yes?). It has been requested for JPA 2.2+ but who knows if Oracle will ever bother to provide that.

    DataNucleus JPA provides 2 extension properties for an embedded field/property

    @Extension(key="null-indicator-column", value="MY_COL")
    @Extension(key="null-indicator-value", value="SomeValue")
    

    and so when the embedded object is null then this column is set to this value, and likewise when reading in objects it can detect a null embedded object and return it correctly to the user.

提交回复
热议问题