11

私のエンティティの構造は次のとおりです。

@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 {}

そして、私は次の例外を得ました:

    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

Intermed クラスで @MappedSuperclass を @Entity に変更すると、すべて正常に動作します。@MappedSuperclass と @SequenceGenerator の使用に問題はありますか? それとも私は何かを逃したのですか?

4

2 に答える 2

13

アプリケーション全体のIDジェネレーターを実現しようとしているときに、この質問で説明されているのと同じ問題に遭遇しました。

解決策は実際には最初の答えにあります。シーケンスジェネレーターを主キーフィールドに配置します

そのようです:

@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 {}

このように物事を行うことは(少なくとも私には)非常にばかげているように見えますが、うまくいきます。

于 2011-06-21T18:11:03.730 に答える
11

JPA 1.0仕様がSequenceGenerator注釈について述べていることは次のとおりです。

9.1.37 SequenceGenerator注釈

アノテーションは、SequenceGeneratorジェネレーター要素がアノテーションに指定されている場合に名前で参照できる主キー ジェネレーターを定義します GeneratedValue。シーケンス ジェネレータは 、エンティティ クラスまたは主キー フィールドまたはプロパティで指定できます。ジェネレーター名のスコープは、永続化ユニットに対してグローバルです (すべてのジェネレーター タイプにわたって)。

また、マップされたスーパークラスはエンティティではありません。したがって、仕様を読んだ方法によると、あなたがやりたいことは不可能です。Intermedクラスをエンティティにするか、サブクラスに配置しますSequenceGenerator

于 2010-08-31T21:55:44.343 に答える