7

Say i have an entity with an auto generated primary key. Now if i try to save the entity with values of all other fields which may not be unique. The entity gets auto populated with the id of the row got inserted. How did it get hold of that primary key value?

EDIT:

If the primary key column is say identity column whose value is totally decided by the database. So it does an insert statement without that column value and the db decides the value to use does it communicate back its decision (I dont think so)

3 Answers 3

7
+50

Hibernate use three method for extracting the DB auto generated field depending on what is support by the jdbc driver or the dialect you are using.

Hibernate extract generated field value to put it back in the pojo :

  1. Using the method Statement.getGeneratedKeys (Statement javadocs)

    or

  2. Inserting and selecting the generated field value directly from the insert statement. (Dialect Javadocs)

    or

  3. Executing a select statement after the insert to retrieve the generated IDENTITY value

All this is done internally by hibernate.

Hope it`s the explication you are looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. Exactly what i was looking for thanks. But i didnt really get the third point. How could it get the identity value with select statement? Given the row values aren't unique except the generated value
It's a function specific to the dialect you are using. The select is not based on a combination of field in the domain object. With Mysql it's use "SELECT LAST_INSERT_ID()" (See [link] (dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html)
1

This section of the Hibernate documentation describes the auto generation of ids. Usually the AUTO generation strategy is used for maximum portability and assuming that you use Annotations to provide your domain metadata you can configure it as follows:

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

Anyway the supplied link should provide all the detail you need on generated ids.

3 Comments

Great but my question was say its an Identity column whose next value is completely decided by the DB, How does hibernate know that the row it update has primary key value x. Edited my question
I'm not sure exactly what you mean but the value of the identifier will be communicated back because it will be set on the domain instance which you've inserted.
Also the value that the database uses for the id depends on the database type. Oracle will use a SEQUENCE, MySQL an auto incrementing value etc.
0

When you create an object with the, say, sequence-derived surrogate primary key, you pass it to the Hibernate session with that field set to the value that Hibernate interprets as "not assigned", by default 0. This field is not populated with the assigned value until the corresponding record is not inserted into the database table. You can trigger insertion by either explicitly calling flush() on the hibernate session or performing a database read in the same session. After that you can check the value of that field and it will be assigned rather than 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.