是“initialValue”的元素Hibernate JPA中不支持@TableGenerator? [英] Is the element of "initialValue" of @TableGenerator not supported in Hibernate JPA?
问题描述
@TableGenerator(name =Emp_Gen,table =ID_GEN,pkColumnName =GEN_NAME,pkColumnValue =Employee_GEN,valueColumnName =GEN_VAL,initialValue = 1000,allocationSize = 100)
没关系,但 initialValue
无效。
下面是名为employee的表(注意:使用MySql,Hibernate-JPA)
我认为第一行'id'是1000,而不是1,对吗?但是,如果它是1,第二个应该是101 ....
谁可以帮我一个愚蠢的男人?
第一个值是1而不是1001是Hibernate的bug HHH-4228 ,状态为不会修复。在你的情况下纠正第一个值是1001而不是1000,因为 initialValue
初始化存储返回的 last 值的列(而不是下一个要返回的值)。
使用persistence.xml中的以下内容(如错误报告中的建议)可以解决第一个值的问题:
< property name =hibernate.id.new_generator_mappingsvalue =true/>
副产品是重新启动应用程序会导致经常出现漏洞:
- initialValue = 1000,allocationSize = 100
- 使用值1001(= valueColumn中的值更新为1100)。
- 关闭并启动应用程序
- 下一个值将为1101,而不是1002。
@TableGenerator(name="Emp_Gen",table="ID_GEN", pkColumnName = "GEN_NAME",pkColumnValue = "Employee_GEN",valueColumnName = "GEN_VAL",initialValue = 1000,allocationSize = 100)
Every is ok,but initialValue
is not effective.
Below is the table named "employee"(Note:MySql,Hibernate-JPA is used)
I think the first row 'id' is 1000,not 1,right?But if it's 1,the second should be 101.... Who can help me a stupid man?
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:
- initialValue = 1000,allocationSize = 100
- Use value 1001 (=> value in valueColumn is updated to 1100).
- shutdown and start application
- next value will be 1101, not 1002.
这篇关于是“initialValue”的元素Hibernate JPA中不支持@TableGenerator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!