Hibernate 4 to 5 migration with existing @TableGenerator

Viewed 664

We migrated from Hibernate 4 to 5, one major problem was the new generator, which generates the IDs for the table rows. Our old generators are all table based, like

@TableGenerator(name = TABLE_NAME + "_generator", table = ...)
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

The new default generator from Hibernate 5 doesn't respect that correctly and generates new ids seemingly "from scratch", so you get "Already existing ID" exceptions when new rows are inserted.

All clean solutions I found were based on the idea that Hibernate 4 was used previously in AUTO-Mode (@GeneratedValue(strategy = GenerationType.AUTO)), but we always used GenerationType.TABLE.

The "dirty" solution: Add <property name="hibernate.id.new_generator_mappings" value="false"/> to Hibernates persistence.xml. The result: Our existing databases can be used without problems, except: Constant warnings from Hibernate

HHH90000015: Found use of deprecated [org.hibernate.id.MultipleHiLoPerTableGenerator] table-based id generator; use org.hibernate.id.enhanced.TableGenerator instead.  See Hibernate Domain Model Mapping Guide for details.]]

My question is: How can we keep the original database, stop using deprecated code, and correctly create new rows with new IDs, not colliding with existing IDs?

Especially since it's said Hibernates 5 pooled identifier generators are better than the table ones: https://vladmihalcea.com/hibernate-hidden-gem-the-pooled-lo-optimizer/

1 Answers

Imagine you have the following mapping:

@Entity
@Table(name = "TST_PATIENT_ENT")
public class Patient
{
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator")
   @TableGenerator(
      name =  "table-generator",
      table = "TST_TABLE_IDENTIFIER",
      pkColumnName = "table_name",
      valueColumnName = "product_id",
      allocationSize = 3
   )
   @Column(name = "pat_id")
   private Long id;
   // ...
}

Now let look at the difference between old style table based IDs generation (org.hibernate.id.MultipleHiLoPerTableGenerator) and new one (org.hibernate.id.enhanced.TableGenerator with pooled-lo, pooled):

  1. MultipleHiLoPerTableGenerator (maxLo = allocationSize - 1)
Inserted IDs   |  Last seq val   |   Count of inserted rows
-----------------------------------------------------------
 1             |      1          |          1
 3, 4          |      2          |          2
 6 .. 8        |      3          |          3
 9 .. 12       |      5          |          4
 15 .. 19      |      7          |          5
  1. org.hibernate.id.enhanced.TableGenerator (pooled-lo)
Inserted IDs   |  Last seq val   |   Count of inserted rows
-----------------------------------------------------------
 1             |      3          |          1
 4, 5          |      6          |          2
 7 .. 9        |      9          |          3
 10 .. 13      |      15         |          4
 16 .. 20      |      21         |          5
  1. org.hibernate.id.enhanced.TableGenerator (pooled)
Inserted IDs   |  Last seq val   |   Count of inserted rows
-----------------------------------------------------------
 1             |      6          |          1
 5, 6          |      9          |          2
 8.. 10        |      12         |          3
 11 .. 14      |      18         |          4
 17 .. 21      |      24         |          5

Where Last seq val is the value:

select product_id from TST_TABLE_IDENTIFIER where table_name = 'TST_PATIENT_ENT';

after each rows insertion.

So, if you decide to use the following configuration:

<property name="hibernate.id.new_generator_mappings">true</property>
<property name="hibernate.id.optimizer.pooled.preferred">pooled-lo</property>

You should review all tables mentioned in the @TableGenerator.table and manually update values of the @TableGenerator.valueColumnName fields to the max(id) + 1. For example, in my case, I should run the following sql:

update TST_TABLE_IDENTIFIER
set product_id = (select max(pat_id) + 1 from TST_PATIENT_ENT)
where table_name = 'TST_PATIENT_ENT';

P.S. Maybe this Steve Ebersole's article will be also useful.

Related