大佬教程收集整理的这篇文章主要介绍了Hibernate,@ SequenceGenerator和allocationSize,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
绝对清楚…您所描述的内容与规范没有 任何 冲突。该规范讨论的是Hibernate分配给您的实体的值,而不是实际存储在数据库序列中的值。
However, there is the option to get the behavior you are looking for. First
see my reply on Is there a way to dynamically choose a @GeneratedValue
strategy using JPA Annotations and
Hibernate? That will give
you the basics. As long as you are set up to use that SequenceStyleGenerator,
Hibernate will interpret alLOCATIOnSize
using the “pooled optimizer” in the
SequenceStyleGenerator. The “pooled optimizer” is for use with databases that
allow an “increment” option on the creation of sequences (not all databases
that support sequences support an increment). Anyway, read up about the
varIoUs optimizer strategIEs there.
我们都知道使用Hibernate时的默认行为@SequenceGenerator
-它使实际数据库序列增加 一@H_801_18@
,将该值乘以50(默认alLOCATIOnSize
值)-然后将该值用作实体ID。
这是错误的行为,并与说明以下内容的规范冲突:
需要明确的是:我不关心生成的ID之间的差距。
我关心与基础数据库序列 不一致的@H_801_18@ ID 。例如:任何其他应用程序(例如,使用纯JDBC)可能要在从序列获得的ID下插入新行-
但所有这些值可能已被Hibernate使用!疯狂。
有人知道任何解决此问题的方法(没有设置alLOCATIOnSize=1
,从而降低性能)吗?
编辑:@H_801_18@
弄清楚。如果最后插入的记录的ID = 1
,则HB同时51,52,53...
在其新实体BUT中使用值:数据库中序列的值将设置为2
。当其他应用程序使用该序列时,很容易导致错误。
另一方面:规范(据我所知)说数据库序列应该设置为51
,同时HB应该使用范围内的值 2,3 ... 50
更新:@H_801_18@
正如下面的史蒂夫·埃伯索尔(Steve
Ebersole)所述:通过设置可以启用我描述的行为(也是许多人中最直观的行为)hibernate.id.new_generator_mappings=true
。
谢谢大家。
更新2:@H_801_18@
对于将来的读者,您可以在下面找到一个有效的示例。
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "USERS_SEQ")
@SequenceGenerator(name = "USERS_SEQ",sequencename = "SEQUENCE_USERS")
private Long id;
}
persistence.xml
<persistence-unit name="testPU">
<properties>
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>
以上是大佬教程为你收集整理的Hibernate,@ SequenceGenerator和allocationSize全部内容,希望文章能够帮你解决Hibernate,@ SequenceGenerator和allocationSize所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。