admin管理员组

文章数量:1125095

public class Base {

    public static final String ID = "id";

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
    @SequenceGenerator(name = "id_gen", initialValue = 1, allocationSize = 1)
    @Column(updatable = false, nullable = false)
    private Long id;
}

@Entity("orders")
public class Order extends Base {}

@Entity("products")
public class Product extends Base {}

I want separate sequence names for Product, Order. How should I do this with JPA @SequenceGenerator and not using Hibernate classes?

Refer: .2/apidocs/javax/persistence/sequencegenerator

Existing code was like this and worked just fine, but its using Hibernate implementation and its deprecated

-    @GenericGenerator(name = "id_gen", type = org.hibernate.id.enhanced.SequenceStyleGenerator.class,
-            parameters = { @Parameter(name = SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, value = "_seq"),
-                    @Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "1"),
-                    @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "1") })
+    @SequenceGenerator(name = "id_gen", sequenceName = "entity_seq", initialValue = 1, allocationSize = 1)
public class Base {

    public static final String ID = "id";

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
    @SequenceGenerator(name = "id_gen", initialValue = 1, allocationSize = 1)
    @Column(updatable = false, nullable = false)
    private Long id;
}

@Entity("orders")
public class Order extends Base {}

@Entity("products")
public class Product extends Base {}

I want separate sequence names for Product, Order. How should I do this with JPA @SequenceGenerator and not using Hibernate classes?

Refer: https://jakarta.ee/specifications/persistence/2.2/apidocs/javax/persistence/sequencegenerator

Existing code was like this and worked just fine, but its using Hibernate implementation and its deprecated

-    @GenericGenerator(name = "id_gen", type = org.hibernate.id.enhanced.SequenceStyleGenerator.class,
-            parameters = { @Parameter(name = SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, value = "_seq"),
-                    @Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "1"),
-                    @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "1") })
+    @SequenceGenerator(name = "id_gen", sequenceName = "entity_seq", initialValue = 1, allocationSize = 1)
Share Improve this question edited 2 days ago user352290 asked 2 days ago user352290user352290 1,3212 gold badges15 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Let' start with annotating class Base with @MappedSuperclass so we don't have to repeat id for every subclass, and remove @SequenceGenerator.

import jakarta.persistence.*;

@MappedSuperclass
public abstract class Base {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
    @Column(updatable = false, nullable = false)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

Now we can apply @SequenceGenerator at class-level. I'm using values for initialValue just for verification in tests.

import jakarta.persistence.Entity;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;

@Entity
@Table(name = "orders")
@SequenceGenerator(name = "id_gen", sequenceName = "orders_seq", initialValue = 10)
public class Order extends Base {
}

@Entity
@Table(name = "products")
@SequenceGenerator(name = "id_gen", sequenceName = "products_seq", initialValue = 20)
public class Product extends Base {
}

Let's verify this with a test

@Test
void sequenceTest() {
    var firstProduct = entityManager.persist(new Product());
    assertEquals(20, firstProduct.getId());

    var firstOrder = entityManager.persist(new Order());
    assertEquals(10, firstOrder.getId());
}

Note that when using Flyway, Postgresql and default allocationSize which is 50, sequences have to be defines like this

CREATE SEQUENCE orders_seq START WITH 1 INCREMENT BY 50 CACHE 50;
CREATE SEQUENCE products_seq START WITH 1 INCREMENT BY 50 CACHE 50;

本文标签: javaHow to make SequenceGenerator generate dynamic sequence names in JPAStack Overflow