0

I have an abstract class annotated as @MappedSuperclass. This class defines attributes common to all JPA classes such as Id.

I would like to override Id attribute mapping defined in the abstract super class and assign a sequence generator. Is it possible to override Id attribute mapping and assign a different sequence generator in JPA 2.x?

Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
Chir
  • 671
  • 1
  • 10
  • 29
  • Short answer: no, [it](http://stackoverflow.com/questions/11667929/jpa-override-auto-generated-id) is not. Or [maybe](http://stackoverflow.com/questions/8589928/mappedsuperclass-change-sequencegenerator-in-subclass). – mabi May 05 '14 at 12:50

1 Answers1

0

One thing that pops into my head is to use two base classes; one without the ID property and one that explicitly adds the ID property. Then you have freedom if you extend the one with ID or the one without ID so you can provide one specifically in the entity. Code skeleton without annotations:

public abstract class _Base {
  // common properties here
}

public abstract class _BaseWithId extends _Base {
  private Long id;
}

public class MyEntity1 extends _BaseWithId {

}

public class MyEntity2 extends _Base {

  private Long id;
}
Gimby
  • 5,095
  • 2
  • 35
  • 47