JPA @TableGenerator shared between multiple entities

Posted by Mauricio on Stack Overflow See other posts from Stack Overflow or by Mauricio
Published on 2010-04-29T23:30:09Z Indexed on 2010/04/29 23:37 UTC
Read the original article Hit count: 438

Filed under:
|
|

Hi Guys, 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) 

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate