Is the element of “initialValue” of @TableGenerator not supported in Hibernate JPA?

后端 未结 1 1347
囚心锁ツ
囚心锁ツ 2021-01-15 10:15

@TableGenerator(name=\"Emp_Gen\",table=\"ID_GEN\", pkColumnName = \"GEN_NAME\",pkColumnValue = \"Employee_GEN\",valueColumnName = \"GEN_VAL\",initialValue = 1000,alloc

相关标签:
1条回答
  • 2021-01-15 10:45

    What it comes to the first value being 1 instead of 1001 that is Hibernate bug HHH-4228, with status Won't fix. Correct first value in your case is 1001 instead of 1000, because initialValue initializes column that stores last value returned (and not the next value to be returned).

    Using following in persistence.xml (as also suggested in bug report) will fix problem with first value:

    <property name="hibernate.id.new_generator_mappings" value="true"/>
    

    Meaning of allocationSize is likely misunderstood in question. It is not step to increment. It means how many values are allocated with one database query from the table. This is rather optimization to avoid additional query every time when id value is needed for new entity.

    Side product is that restart of application causes often holes to the sequence:

    1. initialValue = 1000,allocationSize = 100
    2. Use value 1001 (=> value in valueColumn is updated to 1100).
    3. shutdown and start application
    4. next value will be 1101, not 1002.
    0 讨论(0)
提交回复
热议问题