11

I have following structure of my entities:

@MappedSuperclass
public abstract class BaseEntity {
  @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
  private Long id;
}

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@SequenceGenerator(name = "seqGenerator", sequenceName = "DICTIONARY_SEQ")
public abstract class Intermed extends BaseEntity {}

@Entity
public class MyEntity1 extends Intermed {}

@Entity
public class MyEntity2 extends Intermed {}

And I got following exception:

    Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in class path resource [context/applicationContext.xml]: 
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown Id.generator: seqGenerator

When I change @MappedSuperclass to @Entity on Intermed class, everything works OK. Are there any problems with using @MappedSuperclass and @SequenceGenerator? Or I have missed something?

2 Answers 2

14

I ran into the same problem described in this question while trying to achieve application wide id generators.

The solution is actually in the first answer: put the sequence generator on the primary key field.

Like so:

@MappedSuperclass
public abstract class BaseEntity {
  @Id
  @SequenceGenerator(name = "seqGenerator", sequenceName = "DICTIONARY_SEQ")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
  private Long id;
}

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Intermed extends BaseEntity {}

@Entity
public class MyEntity1 extends Intermed {}

@Entity
public class MyEntity2 extends Intermed {}

While doing things this way seems remarkably stupid (at least to me) it does work.

0
12

Here is what the JPA 1.0 spec says about the SequenceGenerator annotation:

9.1.37 SequenceGenerator Annotation

The SequenceGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence 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).

And a mapped superclass is not an entity. So according to the way I read the spec, what you want to do is not possible. Either make the Intermed class an entity or put the SequenceGenerator on the sub classes.

4
  • the JPA 2.0 is talking about the same thing
    – glaz666
    Sep 1, 2010 at 7:45
  • @glaz666 Hehe :) And indeed, this didn't change in the JPA 2.0 spec, same stuff. Sep 1, 2010 at 8:02
  • 2
    so there is no way to define 1 single "global" sequence for several entities ?
    – radai
    Oct 22, 2010 at 11:51
  • 1
    @radai I put that on the field and it works. See Kallja's answer.
    – Jin Kwon
    Jul 5, 2016 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.