gpt4 book ai didi

java - MappedSuperclass - 子类中的 SequenceGenerator (hibernate 5.2.13.Final)

转载 作者:行者123 更新时间:2023-12-05 05:17:47 24 4
gpt4 key购买 nike

我们尝试将项目形式 5.2.X 中的 hibernate 版本升级到 5.2.13.Final ,但新的 hibernate 正确强制

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

我们的类结构由一个 BaseEntity 和定义了 @GenericGenerator 的子类组成,所有生成器都使用相同的名称。

@MappedSuperclass
public class BaseEntity {
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="idGenerator")
private Long id;
}

@Entity
@Table(name = "SAMPLE_TABLE")
@GenericGenerator(strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", name = "idGenerator", parameters = {
@Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "SAMPLE_SEQ") })
public class SampleEntity extends BaseEntity

是否可以在不在每个类中放置 id 字段的情况下更正此生成器定义?

最佳答案

这对我们在 Hibernate 5.2.1 和 postgres 上有效:

import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.Parameter
import org.hibernate.id.enhanced.SequenceStyleGenerator
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.MappedSuperclass

@MappedSuperclass
abstract class BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@GenericGenerator(name = "sequenceGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = [
Parameter(name = SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, value = "true"),
Parameter(name = SequenceStyleGenerator.OPT_PARAM, value = "pooled"),
Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "1"),
Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "50")
]
)
var id: Long? = null
}

代码在 kotlin 中,但您可以轻松地将其转换为 java。

如果您需要有关 SequenceStyleGenerator 的更多信息,请参阅 docs

关于java - MappedSuperclass - 子类中的 SequenceGenerator (hibernate 5.2.13.Final),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748981/

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