gpt4 book ai didi

java - Hibernate 实体中的多个 SequenceGenerator

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:24 25 4
gpt4 key购买 nike

是否可以在 Hibernate 实体类中使用 2 个序列生成器。我想在我的情况下使用两个序列生成器,一个用于主键,另一个用于简单字段。我怎样才能达到同样的效果?

@Data
@Table(name="a_b")
@SequenceGenerator(name = "a1_seq", sequenceName = "a1_seq", allocationSize = 1)
@SequenceGenerator(name = "b1_seq", sequenceName = "b1_seq", allocationSize = 1)
public class ABC {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a1_seq")
private Integer id;

@Column(name = "c")
private String c;

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "b1")
@Column(name = "b)
private Integer b;

}

最佳答案

您应该只有一个主键 SequenceGenerator:

@Id
@Column(name = "id")
@SequenceGenerator(name = "a1_seq", sequenceName = "a1_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a1_seq")
private Integer id;

对于外键,您可以拥有:

@Column(name = "b, columnDefinition="serial")
private Integer b;

这应该适用于 PostgreSQL。

关于java - Hibernate 实体中的多个 SequenceGenerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28539915/

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