How does hibernate populate auto-generated field ids?

Say I have an object with an automatically generated primary key. Now, if I try to save an object with the values ​​of all other fields that may not be unique. The object is automatically populated with the identifier of the row inserted. How did he get hold of this primary key value?

EDIT:

If the identifier column is specified in the primary key column, the value of which is completely determined by the database. Thus, it executes the insert statement without this column value, and db decides which value to use, whether it binds its solution (I don't think so)

+5
source share
3 answers

Hibernate uses three methods to extract the automatically generated database field, depending on what is supported by the jdbc driver or the dialect used.

Reset hibernation value to return it to pojo:

  • Using the Statement.getGeneratedKeys ( Statement javadocs) Method

    or

  • Insert and select the generated field value directly from the insert statement. ( Dialect Javadocs)

    or

  • Executing a select statement after insertion to get the generated IDENTITY value

All this is done inside sleep mode.

Hope this is the explanation you are looking for.

+6
source

Hibernate . AUTO , , :

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

.

+1

, , , Hibernate , , Hibernate " " 0. , ​​ . flush() , . , 0.

0

All Articles