SrigandanS
SrigandanS

Reputation: 55

How to get the id of saved object by JPA?

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

Answers (1)

user12176589
user12176589

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

Related Questions