Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What strategy is actually selected for "GenerationType.AUTO" for the major databases?

The Hibernate documentations (5.1.2.2. Identifier generator) states

AUTO: selects IDENTITY, SEQUENCE or TABLE depending upon the capabilities of the underlying database.

But I'm unable to find a documentation/overview what @GeneratedValue strategy is used for the specific databases when defining them as GenerationType.AUTO.

Does anybody know whether someone maintains a list of the actual generation strategy for the major databases (e.g. Oracle, DB2, PostgreSQL, MySQL, MSSQL, ...)? And where to find it?

like image 237
xwoker Avatar asked Nov 09 '13 12:11

xwoker


1 Answers

This link is about Java Persistence API and seems regularly updated. http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing

Identity sequencing

Identity sequencing uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase, and PostgreSQL. Oracle does not support IDENTITY columns but its is possible to simulate them using sequence objects and triggers.

Sequence objects

Sequence objects use special database objects to generate ids. Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres. Usually, a SEQUENCE object has a name, an INCREMENT, and other database object settings. Each time the .NEXTVAL is selected the sequence is incremented by the INCREMENT.

Edit

If the database such as DB2 supports both IDENTITY columns and Sequences hibernate chooses Identity columns, see Dialect:

public Class getNativeIdentifierGeneratorClass() {
    if ( supportsIdentityColumns() ) {
        return IdentityGenerator.class;
    }
    else if ( supportsSequences() ) {
        return SequenceGenerator.class;
    }
    else {
        return TableHiLoGenerator.class;
    }
}

You can check what is returned from supportsIdentityColumns() and supportsSequences() for each database by looking at the relevant dialect in the org.hibernate.dialect package.

like image 146
samlewis Avatar answered Oct 03 '22 07:10

samlewis