0

If DB schema forbids certain table columns to hold null is there a way to mark corresponding entity class property with @org.jetbrains.annotations.NotNull (or any other JSR-305 compliant annotation) to provide more information for nullity inference by IDEA inspection (or other static analysis tool)?

When i try to do this inspection reports that property marked with @NotNull must be initialized in class initializer. I guess given this fact it's impossible to achive what i've mentioned above? Anyway JPA provider will create entity class object with uninitialized properties and only fill it with DB data aftewards so from point of view of static analysis tool all properties are always nullable?

Thanks in advance.

yaromir
  • 378
  • 7
  • 17
  • I think your last statement is correct. The NotNull annotations says that the value can not be possibly null at any phase of the class instance's existence. However when you manually create a new instance of your entity class for example, its fields are probably null, so the annotation can't be used there. – Bohuslav Burghardt Mar 07 '15 at 13:49

1 Answers1

0

I'd use @NotNull mainly in APIs to clearly express design intentions. In your case, the ORM is responsible for creating and assembling entities, so you don't really have a way of of altering its mechanisms - just let it do its job and use @NotNull-s where they make sense.

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • API documenting is also a nice example, but talk is about a bit different topic. As for me, IntellijIDEA inspections sometimes find some bugs that i would have overlooked without it, so i want to provide additional information for it to make it do it's job even better. – yaromir Mar 22 '15 at 12:17