1

これは、関連する JPA コードです。

@MappedSuperClass
public abstract class SuperClass {

  @EmbeddedId
  private FileId fileId;

  protected SuperClass() {
  }

  public SuperClass(FileId fileId) {
    this.fileId = fileId;
  }

}

@Embeddable
public class FileId {

  protected FileId() {
  }

  protected File fileName;

  public FileId(File fileName) {
    this.fileName = fileName;
  }

}

@Entity
public MyClass1 extends SuperClass {

  @Id
  protected String id;

  protected MyClass1() {
  }

  public MyClass1(String id, FileId fileId) {
    super(fileId);
    this.id = id;
  }

}

@Entity
public MyClass2 extends SuperClass {

  protected MyClass2() {
  }

  public MyClass2(FileId fileId) {
    super(fileId);
  }

}

実行時に次の例外が発生します。

...
Exception Description: Entity class [class org.abcd.MyClass1] has both an @EmbdeddedId (on attribute [fileId]) and an @Id (on attribute [id]. Both ID types cannot be specified on the same entity.
...

@EmbeddedId 属性 ( @Embeddable クラスのすべての属性) と @Id 属性を一緒に主キーとして定義することは、JPA / Eclipse Link では許可されていないようです。

この問題の可能な解決策を知っている人はいますか?

どんな助けでも大歓迎です。

いくつかの追加情報:

クラス MyClassA には、特定のアーカイブ ファイルに関する情報 (進行状況など。上記のコード例では省略されています) が含まれている必要があります。スーパー クラス SuperClass の属性 fileId を使用して、このファイルを識別します。現在、fileId (id クラスの FileId) はファイル名のみで構成されていますが、後でさらに属性が追加される予定です。

MyClassB には、アーカイブ内のファイルに関する情報が含まれています。このファイルは、属性 ID (アーカイブ内の相対パス) とスーパー クラス SuperClass の fileId で識別されます。

次のデータベース構造を考えます。

テーブル "MyClass1":

ファイル名 | ID | ...

テーブル "MyClass2":

ファイル名 | ...

私が正確に何を望んでいるかがもう少し明確になったことを願っています:)。

4

2 に答える 2

1

あなたのマッピングは意味がありません。同じクラスにan@EmbeddedIdと anがあります。あなたには naがあり、すでに がある SuperClass を拡張します。なぜあなたがそれを望んでいるのかわかりませんか?正確に何を達成しようとしていますか?@IdMyClassA@Id@EmbeddedId

于 2013-01-11T01:26:10.570 に答える
0

みんながあなたに勧めたように-多分データモデルの変更はあなたにここでもっと意味を与えるでしょう。

そして、これが私の簡単な提案です:

IDフィールドとget/set-ttersを含むBasicクラスエンティティ:

@MappedSuperclass
public class Entity { 

     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private int id;

     // getters/setters
} 

これは、モデル内のすべてのエンティティの基本クラスに使用できます。次に、機能要件に従うと、次のことが役立つ可能性があります。

@Entity
@Inheritance(strategy = InheritanceType.JOIN)
public class File extends Entity {

    @Column(unique=true)
    private String fileName;

    // other future columns you mention above
}

@Entity
public class ArchiveFile extends File {

    // other information here - progress and so on 
}

@Entity
public class ArchiveFileTracker extends Entity {

    @OneToMany
    private ArchiteFile architeFile;

    // other specific information here 
}

これがあなたの要件をカバーできることを願っています。

頑張って、シメオン

于 2013-01-11T18:44:18.683 に答える