2

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?

MarkusJackson
  • 225
  • 2
  • 12
  • you can try adding @SequenceGenerator to id attribute. https://docs.jboss.org/hibernate/jpa/2.2/api/javax/persistence/SequenceGenerator.html – e.g78 Nov 12 '20 at 11:55
  • @MarkusJackson What hibernate dialect do you use? Do you use hibernate [schema generation](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#schema-generation)? Do you have a sequence for each table or you use auto increment columns? – SternK Nov 12 '20 at 12:16

1 Answers1

0

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

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58