java 类上的 @SequenceGenerator 用 @MappedSuperclass 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3607691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:38:06  来源:igfitidea点击:

@SequenceGenerator on class annotated with @MappedSuperclass

javahibernateormjpasequence-generators

提问by glaz666

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?

当我在 Intermed 类上将 @MappedSuperclass 更改为 @Entity 时,一切正常。使用@MappedSuperclass 和@SequenceGenerator 有什么问题吗?或者我错过了什么?

采纳答案by Pascal Thivent

Here is what the JPA 1.0 spec says about the SequenceGeneratorannotation:

以下是 JPA 1.0 规范对SequenceGenerator注释的说明:

9.1.37 SequenceGenerator Annotation

The SequenceGeneratorannotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValueannotation. A sequence generator may be specified on the entity classor on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

9.1.37 SequenceGenerator 注解

SequenceGenerator注解定义了可以由名称当用于指定的发电机元件被参考的主键生成 GeneratedValue注释。可以在实体类主键字段或属性上指定序列生成器 。生成器名称的范围对于持久性单元是全局的(跨所有生成器类型)。

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 Intermedclass an entity or put the SequenceGeneratoron the sub classes.

映射的超类不是实体。所以根据我阅读规范的方式,你想做的事情是不可能的。要么使Intermed类成为实体,要么将SequenceGenerator放在子类上。

回答by Kallja

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.

虽然以这种方式做事似乎非常愚蠢(至少对我而言),但它确实有效。