6

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?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
xwoker
  • 3,105
  • 1
  • 30
  • 42

1 Answers1

5

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.

samlewis
  • 3,950
  • 27
  • 27
  • That's interesting information. But it doesn't answer the question e.g. whether Hibernate selects sequence or identity as an strategy for DB2. – xwoker Nov 09 '13 at 13:13
  • I edited my answer with details of how to work this out from the sourcecode. – samlewis Nov 09 '13 at 13:40
  • If you don't specify `hibernate.id.new_generator_mappings` Hibernate uses the native mechanism and I can see how it works. +1 for that. But if you use the recommended setting (with mentioned property set to true), Hibernate will choose some `org.hibernate.id.enhanced.SequenceStyleGenerator` and I have a hard time tracking in the sources where this leads for each of the databases by default. – xwoker Nov 09 '13 at 18:14