Issue
I am confused as everywhere it is mentioned to use @GenerateValue
only with @Id
.
But: can it be used with non primary key attributes?
Solution
The href="https://github.com/javaee/jpa-spec/blob/master/jsr338-MR/JavaPersistence.pdf" rel="nofollow noreferrer">JPA specification document provides an important hint that you should not do so, see section 11.1.20, on page 449:
Footnote [110]: Portable applications should not use the GeneratedValue annotation on other persistent fields or properties.
Here, ”other“ refers to fields or properties of an Entity that are not annotated with @Id
:
The
GeneratedValue
annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with theId
annotation.
Hence, I would conclude that most, if not all, persistence provider implementations might not support your intended (special) use case.
Answered By - MWiesner
Answer Checked By - Katrina (JavaFixing Volunteer)