I am confused as everywhere it is mentioned to use @GenerateValue only with @Id.
But: can it be used with non primary key attributes?
The 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
GeneratedValueannotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with theIdannotation.
Hence, I would conclude that most, if not all, persistence provider implementations might not support your intended (special) use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With