0

I know that we can get the primary key of the newly inserted record with object.getId() when we do hibernate session.save(object). I want to understand how is hibernate getting this id. I want to achieve the same using a plain SQL query instead of using hibernate.

DB Server is MySQL.

DzaveD
  • 13
  • 4
ragi
  • 49
  • 2
  • It depends on which ID generator your entity uses. I think you should forget about Hibernate if what you want to use is JDBC, and think about how you want these IDs to be generated: a sequence? A UUID? An auto-incremented column? something else? – JB Nizet Jan 10 '20 at 09:31

1 Answers1

0

I agree with @JB Nizet about generation: it depends on your annotation and, in any case, the id is assigned to your bean at the commit phase not before so the persistent provider must read the new generated id from database.

Many databases offer a mechanism for doing what is required. MySQL provides a function LAST_INSERT_ID(). More details here or there

Renato
  • 2,077
  • 1
  • 11
  • 22