Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA @TableGenerator shared between multiple entities

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) 
like image 302
Mauricio Avatar asked Apr 29 '10 23:04

Mauricio


1 Answers

Indeed and this is not what I was expecting after reading the javadoc of @TableGenerator:

This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A table generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

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 Dog entity:

@TableGenerator(table = "myseq", name = "dog_gen", pkColumnName = "seq_name", 
      valueColumnName = "seq_val", pkColumnValue = "Dog", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "dog_gen")
private Long id;

And in the Cat, well, the same:

@TableGenerator(table = "myseq", name = "dog_gen", pkColumnName = "seq_name", 
      valueColumnName = "seq_val", pkColumnValue = "Dog", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "dog_gen")
private Long id;
like image 56
Pascal Thivent Avatar answered Oct 01 '22 19:10

Pascal Thivent