是“initialValue”的元素Hibernate JPA中不支持@TableGenerator? [英] Is the element of "initialValue" of @TableGenerator not supported in Hibernate JPA?

查看:394
本文介绍了是“initialValue”的元素Hibernate JPA中不支持@TableGenerator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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/> 

allocationSize 的含义很可能会被误解。它不是增量的步骤。这意味着从表中分配一个数据库查询的值有多少。这是相当优化的,以避免每当新实体需要id值时进行额外的查询。



副产品是重新启动应用程序会导致经常出现漏洞:


  1. initialValue = 1000,allocationSize = 100

  2. 使用值1001(= valueColumn中的值更新为1100)。
  3. 关闭并启动应用程序

  4. 下一个值将为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:

  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.

这篇关于是“initialValue”的元素Hibernate JPA中不支持@TableGenerator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆