Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The differences between GeneratedValue strategies

Tags:

doctrine-orm

In the Doctrine docs they mention that there exists a few different strategies for the @GeneratedValue annotation:

  • AUTO
  • SEQUENCE
  • TABLE
  • IDENTITY
  • UUID
  • CUSTOM
  • NONE

Would someone please explain the differences between all thees strategies?

like image 495
superhero Avatar asked Dec 24 '12 14:12

superhero


People also ask

What is the use of ID GeneratedValue strategy GenerationType identity?

IDENTITY This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence.

What is the use of @GeneratedValue?

Annotation Type GeneratedValue Provides for the specification of generation strategies for the values of primary keys. The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.

What is generator in @GeneratedValue?

@SequenceGenerator defines the generator to be used for generating entity's primary keys. Its name attribute gives it a name to reference it by in the @GeneratedValue annotation. The sequenceName parameter represents the name of the sequence in the actual database.


1 Answers

Check the latest doctrine documentation

Here is a summary : the list of possible generation strategies:

AUTO (default): Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies are IDENTITY for MySQL, SQLite and MsSQL and SEQUENCE for Oracle and PostgreSQL. This strategy provides full portability.

SEQUENCE: Tells Doctrine to use a database sequence for ID generation. This strategy does currently not provide full portability. Sequences are supported by Oracle and PostgreSql and SQL Anywhere.

IDENTITY: Tells Doctrine to use special identity columns in the database that generate a value on insertion of a row. This strategy does currently not provide full portability and is supported by the following platforms:

  • MySQL/SQLite/SQL Anywhere => AUTO_INCREMENT
  • MSSQL => IDENTITY
  • PostgreSQL => SERIAL

TABLE: Tells Doctrine to use a separate table for ID generation. This strategy provides full portability. This strategy is not yet implemented!

NONE: Tells Doctrine that the identifiers are assigned, and thus generated, by your code. The assignment must take place before a new entity is passed to EntityManager#persist. NONE is the same as leaving off the @GeneratedValue entirely.

SINCE VERSION 2.3 :

UUID: Tells Doctrine to use the built-in Universally Unique Identifier generator. This strategy provides full portability.

like image 75
BADAOUI Mohamed Avatar answered Sep 21 '22 23:09

BADAOUI Mohamed