Is using @Nonnull (javax) on entities with Hibernate a bad idea?

195 views Asked by At

We are using Hibernate in our Spring Boot application. Whenever we can, we use @javax.annotation.Nonnull and @javax.annotation.Nullable annotations to help us and the IDE identify, whether we can expect null values from fields/methods/parameters/etc.

For consistency, I would also like to use @Nonnull in @Entity classes whenever I have a table column declared not null. However, some of my colleagues argue that you should not do this as it cannot be guaranteed on entities due to the way Hibernate instantiates objects. One suggestion was to use @Nonnull only on accessors and return requireNonNull(value) on getters.

What is your opinion on the matter? Or maybe it is not an opinion, and they are just right. What is the right way to be null-safe when accessing entity properties?

PS: I could not find anything on the topic that does not deal with validation, which is not the intention here.

0

There are 0 answers