@SequenceGenerator and single id in whole database



I have a question about @SequenceGenerator. I have separate @SequenceGenerator in each of my model enitities, but it looks like id's are generated using only one, shared generator.

Is there any other way to use separate generators for each entity, so that each entity uses its own sequence just like in RDBM?


You can use different generators for different entity classes:

  • By using multiple @SequenceGenerator and/or @TableGenerator definitions (with different name values), and attaching these generators to @Id fields in different classes. Make sure you set both the strategy and the generator attributes of the @GeneratedValue annotation.

  • By using the IDENTITY strategy without specifying generators explicitly (in this case an automatic generator is created per class hierarchy).

More details are provided in the manual.

ObjectDB Support

Thanks, I think I miss the part about strategy type. Each of my model class got following annotations:

@SequenceGenerator(name = "XXX", sequenceName = "Seq_XXX", initialValue = 1000)
@GeneratedValue(generator = "XXX")
private long id;

As I understand default strategy is Auto, which causes global ID's per database, not per class?



Yes, that is correct. The generator is ignored when strategy is AUTO (default).

Maybe this should be changed and AUTO should use a generator if specified.

ObjectDB Support