简体   繁体   English

使用@MappedSuperclass注释的类上的@SequenceGenerator

[英]@SequenceGenerator on class annotated with @MappedSuperclass

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. 当我在Intermed类上将@MappedSuperclass更改为@Entity时,一切正常。 Are there any problems with using @MappedSuperclass and @SequenceGenerator? 使用@MappedSuperclass和@SequenceGenerator有什么问题吗? Or I have missed something? 或者我错过了什么?

I ran into the same problem described in this question while trying to achieve application wide id generators. 在尝试实现应用程序范围的id生成器时,我遇到了此问题中描述的相同问题。

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. 虽然以这种方式做事似乎非常愚蠢(至少对我来说)确实有效。

Here is what the JPA 1.0 spec says about the SequenceGenerator annotation: 以下是JPA 1.0规范关于SequenceGenerator注释的内容:

9.1.37 SequenceGenerator Annotation 9.1.37 SequenceGenerator注释

The SequenceGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. SequenceGenerator注释定义了一个主键生成器,当为GeneratedValue批注指定了GeneratedValue器元素时,该生成器可以通过名称引用。 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. 要么将Intermed类设为实体,要么将SequenceGenerator放在子类上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM