问题
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