Reputation: 55
I want to get the ID of the object saved to the database by JPA. Consider an object person
, I save this object using
personrepo.save(person)
How can I get the ID of this object?
Note: ID is the only unique column in this object.
Upvotes: 1
Views: 1653
Reputation: 107
If you work with Spring Data, method save returns persisted object with generated ID. So:
person = personrepo.save(person);
var id = person.getId();
For more details, see documentation.
Upvotes: 4