Hibernate: Own ID-Generator Sequence for every entity

710 Views Asked by At

I currently use this approach for generating ids:

@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

The effect is, when I save enity of type A it gets the id '1', then I save Enity B and it gets id '2' (instead of 1, because its another entity type), then I save an entity of type A again and it gets id '3' (instead of 2).

What I would like is, that Hibernate counts the ID sequence for every Entity-Type for its own. Is that possible?

1

There are 1 best solutions below

0
Christian Beikov On

Just define different sequences for every entity type. Also see https://docs.oracle.com/javaee/6/api/javax/persistence/SequenceGenerator.html