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

前端 未结 1 354
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 03:24

The Hibernate documentations (5.1.2.2. Identifier generator) states

AUTO: selects IDENTITY, SEQUENCE or TABLE depending upon the capabilities of the under

1条回答
  •  北海茫月
    2021-01-21 03:49

    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.

    0 讨论(0)
提交回复
热议问题