Please use the Apache issue tracking system for new NetBeans issues ( !!
Bug 197845 - Contradictory Bean Validation constraints on JPA entity
Contradictory Bean Validation constraints on JPA entity
Product: javaee
Classification: Unclassified
Component: Persistence
All All
: P3 (vote)
: 7.2
Assigned To: Sergey Petrov
Depends on:
  Show dependency treegraph
Reported: 2011-04-18 18:29 UTC by arungupta
Modified: 2012-05-16 11:26 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT


Note You need to log in before you can comment on or make changes to this bug.
Description arungupta 2011-04-18 18:29:21 UTC
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.
Comment 1 David Konecny 2011-04-19 01:21:46 UTC
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?
Comment 2 donatasc 2012-05-14 12:31:48 UTC
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???
Comment 3 David Konecny 2012-05-14 22:48:52 UTC
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.
Comment 4 Sergey Petrov 2012-05-15 12:35:53 UTC
ok, will remove for GenerationType.IDENTITY
Comment 5 Sergey Petrov 2012-05-15 15:37:40 UTC
Comment 6 Quality Engineering 2012-05-16 11:26:54 UTC
Integrated into 'main-golden', will be available in build *201205160400* on (upload may still be in progress)
User: Sergey B. Petrov <>
Log: #197845  - remove NotNull from autoincrement fields

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo