@SequenceGenerator and single id in whole database

#1

Hi,

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?

#2

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

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")
@Id
private long id;

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

 

#4

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
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login