When using Hibernate it quickly comes to the point, where you have to decide how your @Id attribute of your entity should be generated. JPA offers four possible options
- IDENTITY
- SEQUENCE
- TABLE
- AUTO
For a description of these see the Javadoc of GenerationType.
If you have decided to take IDENTITY or SEQUENCE in the past, this can be a problem when supporting new databases in the future. Sadly, most of the databases don’t support both GenerationTypes. That is a bummer, because you have to decide early during development which GenerationType to choose. No matter which you’ve chosen, it’s the wrong one if it’s not TABLE or AUTO :).
If you now think, simply changing the GenerationType to AUTO fixes the problem, you can get into serious trouble!
Imagine your application used IDENTITY in the past and already supported MySQL and MSSQL as possible databases. Now a new requirement comes in to support Oracle as well. The first you encounter will be an exception from Hibernate, telling you that Oracle doesn’t support the IDENTITY generation. What comes next to your mind may be, that simply switching to AUTO will fix the problem. Since Hibernate will choose the “right” generator depending on the database. But what if the “right” generator for the already existing databases like MySQL and MSSQL isn’t IDENTITY? It surely won’t work with your existing production databases anymore – and might even damage those!
So before switching to AUTO, you should make sure that the chosen GenerationType in the past (e.g. IDENTITY) matches the current Hibernate “native” generator for the used Dialects. This is because Hibernate maps JPA GenerationType.AUTO to its “native” generators.
Since I haven’t found any documentation about Dialects and their AUTO/”native” generators – not even in the Hibernate documentation – I’ve created a little matrix that shows the results for each Dialect:
[br]
[expand title=”Hibernate 3.3.1 which ships with JBoss 5.1.0″]
Dialect | Supports Identity | Supports Sequence | AUTO/native |
---|---|---|---|
Cache71Dialect | true | false | IdentityGenerator |
DB2390Dialect | true | false | IdentityGenerator |
DB2400Dialect | true | false | IdentityGenerator |
DB2Dialect | true | true | IdentityGenerator |
DataDirectOracle9Dialect | false | true | SequenceGenerator |
DerbyDialect | true | false | TableHiLoGenerator |
FirebirdDialect | false | true | SequenceGenerator |
FrontBaseDialect | false | false | TableHiLoGenerator |
H2Dialect | true | true | IdentityGenerator |
HSQLDialect | true | true | IdentityGenerator |
InformixDialect | true | true | IdentityGenerator |
IngresDialect | false | true | SequenceGenerator |
InterbaseDialect | false | true | SequenceGenerator |
JDataStoreDialect | true | false | IdentityGenerator |
MckoiDialect | false | true | SequenceGenerator |
MimerSQLDialect | false | true | SequenceGenerator |
MySQL5Dialect | true | false | IdentityGenerator |
MySQL5InnoDBDialect | true | false | IdentityGenerator |
MySQLDialect | true | false | IdentityGenerator |
MySQLInnoDBDialect | true | false | IdentityGenerator |
MySQLMyISAMDialect | true | false | IdentityGenerator |
Oracle10gDialect | false | true | SequenceGenerator |
Oracle8iDialect | false | true | SequenceGenerator |
Oracle9Dialect | false | true | SequenceGenerator |
Oracle9iDialect | false | true | SequenceGenerator |
OracleDialect | false | true | SequenceGenerator |
PointbaseDialect | false | false | TableHiLoGenerator |
PostgreSQLDialect | true | true | SequenceGenerator |
ProgressDialect | false | false | TableHiLoGenerator |
RDMSOS2200Dialect | false | true | SequenceGenerator |
SAPDBDialect | false | true | SequenceGenerator |
SQLServerDialect | true | false | IdentityGenerator |
Sybase11Dialect | true | false | IdentityGenerator |
SybaseAnywhereDialect | true | false | IdentityGenerator |
SybaseDialect | true | false | IdentityGenerator |
TeradataDialect | false | false | TableHiLoGenerator |
TimesTenDialect | false | true | SequenceGenerator |
[/expand]
[br]
[expand title=”Hibernate 4.0.1 which ships with JBoss 7.1.1″]
Dialect | Identity | Sequence | AUTO/native |
---|---|---|---|
CUBRIDDialect | false | true | SequenceGenerator |
Cache71Dialect | true | false | IdentityGenerator |
DB2390Dialect | true | false | IdentityGenerator |
DB2400Dialect | true | false | IdentityGenerator |
DB2Dialect | true | true | IdentityGenerator |
DataDirectOracle9Dialect | false | true | SequenceGenerator |
DerbyDialect | true | false | IdentityGenerator |
DerbyTenFiveDialect | true | false | IdentityGenerator |
DerbyTenSevenDialect | true | true | IdentityGenerator |
DerbyTenSixDialect | true | true | IdentityGenerator |
FirebirdDialect | false | true | SequenceGenerator |
FrontBaseDialect | false | false | TableHiLoGenerator |
H2Dialect | true | true | IdentityGenerator |
HSQLDialect | true | true | IdentityGenerator |
InformixDialect | true | true | IdentityGenerator |
Ingres10Dialect | true | true | IdentityGenerator |
Ingres9Dialect | false | true | SequenceGenerator |
IngresDialect | false | true | SequenceGenerator |
InterbaseDialect | false | true | SequenceGenerator |
JDataStoreDialect | true | false | IdentityGenerator |
MckoiDialect | false | true | SequenceGenerator |
MimerSQLDialect | false | true | SequenceGenerator |
MySQL5Dialect | true | false | IdentityGenerator |
MySQL5InnoDBDialect | true | false | IdentityGenerator |
MySQLDialect | true | false | IdentityGenerator |
MySQLInnoDBDialect | true | false | IdentityGenerator |
MySQLMyISAMDialect | true | false | IdentityGenerator |
Oracle10gDialect | false | true | SequenceGenerator |
Oracle8iDialect | false | true | SequenceGenerator |
Oracle9Dialect | false | true | SequenceGenerator |
Oracle9iDialect | false | true | SequenceGenerator |
OracleDialect | false | true | SequenceGenerator |
PointbaseDialect | false | false | TableHiLoGenerator |
PostgreSQLDialect | true | true | SequenceGenerator |
PostgresPlusDialect | true | true | SequenceGenerator |
ProgressDialect | false | false | TableHiLoGenerator |
RDMSOS2200Dialect | false | true | SequenceGenerator |
SAPDBDialect | false | true | SequenceGenerator |
SQLServer2005Dialect | true | false | IdentityGenerator |
SQLServer2008Dialect | true | false | IdentityGenerator |
SQLServerDialect | true | false | IdentityGenerator |
Sybase11Dialect | true | false | IdentityGenerator |
SybaseASE157Dialect | true | false | IdentityGenerator |
SybaseASE15Dialect | true | false | IdentityGenerator |
SybaseAnywhereDialect | true | false | IdentityGenerator |
SybaseDialect | true | false | IdentityGenerator |
TeradataDialect | false | false | TableHiLoGenerator |
TimesTenDialect | false | true | SequenceGenerator |
[/expand]
[br]
If you would like to check other versions of Hibernate, simply download the Maven/Eclipse project, change the Hibernate version in the pom, and call “mvn test” to get the results.
In the meantime, the Hibernate documentation mentions the problems with portability of id generation and recommends to use the enhanced identifier generators for new projects.