JPA Entity class giving error with 2 @GeneratedValue fields

后端 未结 2 1360
遥遥无期
遥遥无期 2021-01-28 13:25

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 @GeneratedVa

相关标签:
2条回答
  • 2021-01-28 13:54

    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;
    
    0 讨论(0)
  • 2021-01-28 14:02

    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.

    0 讨论(0)
提交回复
热议问题