2

I am new to Spring framework and trying to implement a simple CRUD application in spring boot with MySQL as database. Everything is working fine. I have the Auto Increment enabled on Id field in the database. I am using EntityManager.persist() method to save the data in database and it is working fine. Now I want to return the auto generatedId back to the client as response of POST method but EntityManager.persist()return type is void.

Can anyone help me that how I can return the Id back?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Waqas Ali Razzaq
  • 659
  • 1
  • 5
  • 30

1 Answers1

1

the id is guaranteed after flush operation or when the transaction is completed.

    em.persist(employee)
    em.flush();
    long id = employee.getId();

for more details read

What's the advantage of persist() vs save() in Hibernate?

khurram
  • 31
  • 2