This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 197845

Summary: Contradictory Bean Validation constraints on JPA entity
Product: javaee Reporter: arungupta <arungupta>
Component: PersistenceAssignee: Sergey Petrov <sj-nb>
Status: RESOLVED FIXED    
Severity: normal CC: dkonecny, donatasc, pjiricka
Priority: P3    
Version: 7.0   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:

Description arungupta 2011-04-18 18:29:21 UTC
The following set of constraints on a JPA entity are contradictory:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull

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:

https://forum.hibernate.org/viewtopic.php?f=9&t=1011908

https://hibernate.onjira.com/browse/BVAL-234

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
http://hg.netbeans.org/web-main/rev/613df0f08616
Comment 6 Quality Engineering 2012-05-16 11:26:54 UTC
Integrated into 'main-golden', will be available in build *201205160400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/613df0f08616
User: Sergey B. Petrov <sj-nb@netbeans.org>
Log: #197845  - remove NotNull from autoincrement fields