verma_neeraj
verma_neeraj

Reputation: 21

JPA Entity class giving error with 2 @GeneratedValue fields

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

Upvotes: 0

Views: 4091

Answers (2)

FilippoG
FilippoG

Reputation: 339

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;

Upvotes: 0

Alex Minjun Yu
Alex Minjun Yu

Reputation: 3707

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.

Upvotes: 1

Related Questions