The following set of constraints on a JPA entity are contradictory:
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
The primary key is to auto-generated and thus will be null when EntityManager.persist is invoked. But prePersist throws a BeanValidation exception because of it being null. When @GeneratedValue(strategy=GenerationType.IDENTITY), then @NotNull should not be specified.
I disagree. I think it is a defect in Beans Validation integration into Persistence. The prePersist should be smart enough to ignore NotNull when GeneratedValue is present. Compare it with preUpdate and preRemove: NotNull is perfectly valid there and make sense. Would you agree Arun? And if so could you file this issue for Persistence Specification to fix this and clarify?
Well this is resolved prematurely. Bean Validation team has different opinion:
And this is a real headache - most of my entities in DB have IDENTITY auto-generation, I have to remove @NotNull each time I reverse-engineer the database.
@Id properties in DB are primary keys - DB itself controls their nullability on insert/update - why to generate @NotNull???
donatasc, thanks for the feedback and pointers to the discussions about this issue.
I was not aware of problems this issue is causing as you are first user (not counting Arun :-) reporting this problem to us. In that light I suggest that in short term NetBeans do not generate @NotNull annotation - we can fix this for NetBeans 7.2. Sergey, if you agree could you fix this please?
Regarding what is the right long term solution - I do not know. I tried to re-read relevant parts of involved specs but there is nothing which would clearly answer our question. I like your proposal that @Id annotation should control nullability. That makes sense but that's not the case currently - @Id as defined in Java Persistence spec is not annotated with @Constraint and therefore it is not a constraint in Bean Validation spec sense. On the other hand Java Persistence spec in chapter "3.6.1" says "This specification supports the use of bean validation for the automatic validation of entities upon the
pre-persist, pre-update, and pre-remove lifecycle validation events." which I interpret literally - Java Persistence defines some lifecycles events like pre-persist, pre-update, etc. and for these events *it is using* Bean Validation to execute validation. So it is Java Persistence job to do validation not Bean Validation and therefore I would think that Java Persistence should be able to handle NotNull correctly in presence of its own annotations like @Id and/or @GeneratedValue. Appendix D in Bean Validation specification also suggests that Java Persistence providers should understand @NotNull annotation.
Having said all that this is only my personal interpretation of the specs and I can be wrong.
Thanks once again for talking back to us about this.
ok, will remove for GenerationType.IDENTITY
Integrated into 'main-golden', will be available in build *201205160400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
User: Sergey B. Petrov <firstname.lastname@example.org>
Log: #197845 - remove NotNull from autoincrement fields