JPA 实体 class 使用 2 个 @GeneratedValue 字段给出错误

JPA Entity class giving error with 2 @GeneratedValue fields

我有两列将使用@GeneratedValues,但是当我这样放置它们时出现错误; “异常说明:Class [class 测试] 有两个@GeneratedValues:用于字段[Testing.SEQ_NO] 和[Testing.ID]。只允许一个。“

@Table(name = "TABLE1")
@Entity
public class Testing{

@GeneratedValue(strategy=GenerationType.AUTO)
@Id
private Long id;

@Column(name = "LINKAGE_ID")
private int linkageId;

@Column(name = "TRANSFER_ID")
private int transferId;

@Column(name = "STATUS")
private String status;

@Column(name = "COMMENTS")
private String comments;

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ")
@SequenceGenerator(name="SEQ",sequenceName="SEQ", allocationSize=1)
@Column(name = "SEQ_NO")
private int seqNo;



**I have also created a simple sequence in Db using this:**


CREATE SEQUENCE SEQ START WITH 1

如错误消息所述,只允许一个带有 @GeneratedValue 的字段,但您有两个。
请删除其中一个。

恐怕你不能通过简单的注释来达到你的目的。
查看现有的 post 解决方法。

不确定为什么需要在同一 table 中包含两列,其值需要自动递增。
如果你真的想要两个 Unique 列,你可以像往常一样使用你的 id 和 UUID 用于另一列。

将 columnDefinition="serial" 与 Postgresql 一起使用对我有用。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition="serial")
@Generated(GenerationTime.INSERT)
private Long seqNo;