Thursday, September 8, 2011

Upgrade Hibernate Core 3.6 to 4.0

Be careful with updating Hibernate Core 3.6 to 4.0 with MySQL as database and strategy GenerationType.AUTO. For me it was an update of JBoss AS 6 (Hibernate 3.6) to AS 7 (Hibernate 4). If you have mapped tables like this:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Hibernate 3.6 uses the auto increment functionality to increment the id. But Hibernate 4 creates a sequence table and uses this to increment the id. Even if you say sequences are ok, you will run into exceptions on a given database, since Hibernate tries to save the first object with id 1 which already exists. Better change the strategy to:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Now both Hibernate versions will create the table structure in the same way.

No comments:

Post a Comment