How does the JPA @SequenceGenerator annotation work

前端 未结 5 1184
轮回少年
轮回少年 2020-11-27 03:43

I am learning JPA and have confusion in the @SequenceGenerator annotation.

To my understanding, it automatically assigns a value to the numeric identity

5条回答
  •  感情败类
    2020-11-27 04:23

    sequenceName is the name of the sequence in the DB. This is how you specify a sequence that already exists in the DB. If you go this route, you have to specify the allocationSize which needs to be the same value that the DB sequence uses as its "auto increment".

    Usage:

    @GeneratedValue(generator="my_seq")
    @SequenceGenerator(name="my_seq",sequenceName="MY_SEQ", allocationSize=1)
    

    If you want, you can let it create a sequence for you. But to do this, you must use SchemaGeneration to have it created. To do this, use:

    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    

    Also, you can use the auto-generation, which will use a table to generate the IDs. You must also use SchemaGeneration at some point when using this feature, so the generator table can be created. To do this, use:

    @GeneratedValue(strategy=GenerationType.AUTO)
    

提交回复
热议问题