JPA Entity class giving error with 2 @GeneratedValue fields

霸气de小男生 提交于 2021-02-17 05:11:12

问题


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

回答1:


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

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.




回答2:


Using columnDefinition="serial" with Postgresql it works for me.

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

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


来源:https://stackoverflow.com/questions/42129330/jpa-entity-class-giving-error-with-2-generatedvalue-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!