I would very much appreciate your advice on the JPA. We have a database (PostgreSQL engine), and for tables it happens that a column has its DEFAULT. The database is connected to a Java backend (with JPA and Hibernate). What is the correct way to force JPA not to try to insert IDs for the primary keys that are generated by the table column DEFAULT?
I’ve tried the settings below, but unfortunately I’m having no luck:
Thank you in advance for any advice 🙂
I tried to search for a solution on the internet and in the documentation, but unfortunately, without success.
2
Answers
Assuming that this is Hibernate 6 (you have not stated), the annotation you’re looking for is
@Generated
, and it’s documented here:https://docs.jboss.org/hibernate/orm/6.2/javadocs/org/hibernate/annotations/Generated.html
I also tweeted an example almost exactly like yours here:
https://twitter.com/1ovthafew/status/1645796021585457153
So, something like:
should work.
You should use the annotation
@GenerateValue
to let JPA not use the default generate strategy.The documentation is here: https://www.baeldung.com/hibernate-identifiers