25.4.1. Identifiers
<div class="paragraph">
When it comes to identifiers, you can either choose a natural id or a synthetic key.
</div>
<div class="paragraph">
For natural identifiers, the **assigned** identifier generator is the right choice.
</div>
<div class="paragraph">
For synthetic keys, the application developer can either choose a randomly generates fixed-size sequence (e.g. UUID) or a natural identifier.
Natural identifiers are very practical, being more compact than their UUID counterparts, so there are multiple generators to choose from:
</div>
<div class="ulist">
IDENTITY
SEQUENCE
TABLE
</div>Although the
TABLE
generator addresses the portability concern, in reality, it performs poorly because it requires emulating a database sequence using a separate transaction and row-level locks. For this reason, the choice is usually betweenIDENTITY
andSEQUENCE
.</div>
</td>
If the underlying database supports sequences, you should always use them for your Hibernate entity identifiers.
</div>
Only if the relational database does not support sequences (e.g. MySQL 5.7), you should use the
IDENTITY
generators. However, you should keep in mind that theIDENTITY
generators disables JDBC batching forINSERT
statements.</div> </td> </tr> </table> </div>
If you’re using the
SEQUENCE
generator, then you should be using the enhanced identifier generators that were enabled by default in Hibernate 5. The pooled and the pooled-lo optimizers are very useful to reduce the number of database roundtrips when writing multiple entities per database transaction.</div> </div>