@SequenceGenerator on class annotated with @MappedSuperclass

前端 未结 2 1666
情话喂你
情话喂你 2021-02-08 03:38

I have following structure of my entities:

@MappedSuperclass
public abstract class BaseEntity {
  @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generat         


        
2条回答
  •  天涯浪人
    2021-02-08 04:06

    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.

提交回复
热议问题