The default identifier generator for Postgresql in Hibernate is SequenceGenerator [1].
i.e. Hibernate will do SELECT nextval('hibernate_sequence')
to generate an ID before doing an INSERT foo (id, ...) VALUES (123, ...)
on session commit.
However, PostgreSql supports autoincrement id columns (see e.g. [2]), and the default generator for all other databases which support autoincrement is to use that feature [3], and to perform inserts omitting the id value and to query the database for the new id (before session commit, but within the session's transaction).
I have seen some recent discussion [4] suggesting that the former strategy is better in general, due to the insert-before-session-commit mismatch.
If SequenceGenerator is better (as per [4]), why is it not the default for databases which support it (see [3])?
If IdentityGenerator is better, why does PostgreSql explicitly choose SequenceGenerator when Postgres does support the former (as per [2])?
I tried to find the history of the decision to override the default in the Postgres dialect (see [1]), but I couldn't find the relevant commit in GitHub. I have followed the code back to the SVN repository, but the trail goes cold where the PostgreSQLDialect file is added at r11563 with an unhelpful commit message of "maven migration" [5]. I can't seem to follow the history any further back. Can anyone find the commit which added this override? Perhaps there is more info in the commit message.
Thanks in advance.
[4] http://nhforge.org/blogs/nhibernate/archive/2009/03/20/nhibernate-poid-generators-revealed.aspx
serial
datatype is just a "sequence in disguise". – Burck