gpt4 book ai didi

java - 如何为所有实体定义 allocationSize 和 initialValue

转载 作者:行者123 更新时间:2023-11-29 03:18:22 24 4
gpt4 key购买 nike

在我所有的实体上,我手动定义了 allocationSizeinitialValue,就像这样:

@Id
@SequenceGenerator(name = "ID_GENERATOR", sequenceName = "SEQUENCE_ID", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
@Column(name = "ID")
private Long id;

是否可以为我的所有实体全局设置这些属性?如果是,怎么办?

最佳答案

根据SequenceGenerator's documentation :

The scope of the generator name is global to the persistence unit (across all generator types).

因此您可以在 package-info.java 中定义一个,如 reference documentation 中所述:

@GenericGenerators({
@GenericGenerator(
name = "uuid2",
strategy = "uuid2"
)
})
package your.package;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.GenericGenerators;

然后您需要将您的包添加到 SessionFactory 配置中:

configuration.addPackage("your.package");  

然后您可以在所有实体中重用这些通用生成器:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
@Column(name = "ID")
private Long id;

关于java - 如何为所有实体定义 allocationSize 和 initialValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com