I have a ‘dog’ Entitiy with an @Id and a @TableGenerator
...
@TableGenerator(table = "seq", name = "dog_gen", pkColumnName = "seq_name", valueColumnName="seq_val")
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "dog_gen")
private Long id;
...
Is there a way to reuse the same table generator (dog_gen) in other entity? I want to keep the same id sequence in two independent Entities, say
dog=1,
dog=2,
dog=3,
cat=4,
cat=5,
dog=6
and so on…
Both entities don’t share a common superclass to implement some kind of inheritance with the id property.
If I add the @GeneratedValue( generator=”dog_gen”) on my cat entity, omitting the @TableGenerator declaration throws an Exception saying it can’t find the generator when starting the context.
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: dog_gen
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:413)
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1795)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1229)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:498)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:277)
Indeed and this is not what I was expecting after reading the javadoc of
@TableGenerator:Maybe I’m not interpreting things correctly (the section 9.1.38 of the JPA spec doesn’t tell us more) but I was expecting to be able to reference a generator name from another Entity in the
@GeneratedValue.Anyway, the following seems to work. In the
Dogentity:And in the
Cat, well, the same: