@SequenceGenerator用@MappedSuperclass注解类 [英] @SequenceGenerator on class annotated with @MappedSuperclass

查看:355
本文介绍了@SequenceGenerator用@MappedSuperclass注解类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体具有以下结构:

  @MappedSuperclass 
公共抽象类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 { }

我得到以下异常:



<$由于:org.springframework.beans.factory.BeanCreationException:
在类路径资源[context / applicationContext.xml]中定义名称为'sessionFactory'的bean时出错:
调用init方法失败;嵌套异常是org.hibernate.AnnotationException:未知Id.generator:seqGenerator

当我将@MappedSuperclass更改为Intermed类的实体,一切正常。
使用@MappedSuperclass和@SequenceGenerator是否有问题?或者我错过了一些东西? 这是JPA 1.0规范中关于 SequenceGenerator code> annotation:

lockquote

9.1.37 SequenceGenerator Annotation

SequenceGenerator 注释
定义了一个主键生成器,当为
指定
生成器元素时,可以通过名称引用
GeneratedValue 注释。
序列生成器可以在实体类或上的主键字段或属性中指定

生成器名称的
范围是持久化单元的全局
(跨所有
生成器类型)。

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


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?

解决方案

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.

这篇关于@SequenceGenerator用@MappedSuperclass注解类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆