Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can @GenerateValue be used with non primary key attributes?

I am confused as everywhere it is mentioned to use @GenerateValue only with @Id.

But: can it be used with non primary key attributes?

like image 651
Mohammed azam Varapakula Shaik Avatar asked Oct 24 '25 14:10

Mohammed azam Varapakula Shaik


1 Answers

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 GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.

Hence, I would conclude that most, if not all, persistence provider implementations might not support your intended (special) use case.

like image 173
MWiesner Avatar answered Oct 27 '25 04:10

MWiesner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!