Mapping a primary key column with JPA and Hibernate is simple. You just need to add an attribute to your entity, make sure that its type and name match the database column, annotate it with @Column and you’re done. You can then use the primary key to load the entity, and Hibernate sets the primary key value automatically. And if you want to persist a new entity, you need to set the primary key value programmatically.
But with JPA and Hibernate you can do much more than that. You can:
- choose between different strategies to generate unique primary key values,
- use UUIDs and generate their values,
- map composite primary keys,
- share primary key values across associations and
- map natural IDs.