JPA or Hibernate to generate a (non primary key) column value, not starting from 1

前端 未结 3 1199
别跟我提以往
别跟我提以往 2021-01-06 06:10

I want a JPA/Hibernate (preferably JPA) annotation that can generate the value of a column, that is not a primary key and it doesn\'t start from 1.

From what I have

3条回答
  •  难免孤独
    2021-01-06 06:59

    The @GeneratedValue only works for identifiers and so you can't use it. If you use MySQL, you are quite limited, since database sequences are not supported.

    InnoDB doesn't support multiple AUTO_INCREMENT columns and if your table PK is AUTO_INCREMENTED, then you have two options:

    1. Go for a separate table that behaves like a sequence generator, the solution you already said you are not happy about.

    2. Use an INSERT TRIGGER to increment that column.

提交回复
热议问题