JPA实体类给出了带有2个@GeneratedValue字段的错误 [英] JPA Entity class giving error with 2 @GeneratedValue fields

查看:154
本文介绍了JPA实体类给出了带有2个@GeneratedValue字段的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列将使用@GeneratedValues,但是当我像这样放置它们时,会出现错误; 异常描述:类[class Testing]具有两个@GeneratedValues:用于字段[Testing.SEQ_NO]和[Testing.ID].仅允许一个."

I have two columns which will use @GeneratedValues, but when I am putting them like this it is giving error; " Exception Description: Class [class Testing] has two @GeneratedValues: for fields [Testing.SEQ_NO] and [Testing.ID]. Only one is allowed. "

@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的字段,但是您有两个.
请删除其中之一.

Like the error message says, Only one field with @GeneratedValue is allowed but you have two.
Please remove one of them.

恐怕您无法通过简单的注释来完成您想要的操作.
请查看此现有帖子以找到解决方法.
解决方法

I am afraid you can't do what you intended by simple annotations.
Check out this existing post for workaround.
workaround

不确定在同一表中为什么需要两列,它们的值需要自动递增.
如果您确实想要两个唯一"列,则可以像往常一样使用您的ID,并为另一个列使用UUID.

Not sure why you need two columns in same table, whose value need to be auto incremented.
If you really want two Unique columns, you can use your id as usual and UUID for the other column.

这篇关于JPA实体类给出了带有2个@GeneratedValue字段的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆