简体   繁体   中英

JPA - @EmbeddedId and @Id

This is the relevant JPA code:

@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);
  }

}

At runtime I get the following exception:

...
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.
...

It seems that defining a @EmbeddedId attribute (all attributes of the @Embeddable class) and a @Id attribute together as primary key is not allowed in JPA / Eclipse Link.

Does anybody know a possible solution for this problem?

Any help is appreciated.

Some additional information:

The class MyClassA should contain information (progress, ...; ommited in the code example above) about a certain archive file; the attribute fileId of the super class SuperClass is used to identify this file. Currently fileId (the id-class FileId) only consists of the file name, but later more attributes will be added.

MyClassB contains information about a file IN the archive. This file will be identified with the atttribute id (its the relative path in the archive) and fileId of the super class SuperClass.

I think of the following database structure:

Table "MyClass1":

fileName | id | ...

Table "MyClass2":

fileName | ...

I hope now it's a bit more clear what I exactly want :).

Your mapping doesn't make any sense. You DO have an @EmbeddedId and an @Id in the same class: your MyClassA has na @Id and it extends a SuperClass which already has an @EmbeddedId . I don't know why you would want that? What exactly are you trying to accomplish?

As guys recommended you - maybe changing of the data models will give you more sense here.

And here is my simple proposal :

A Basic class entity containing an ID field and get/set-tters like :

@MappedSuperclass
public class Entity { 

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

     // getters/setters
} 

This could be used for basic class for all entities in you model. Then following your functional requirements maybe next could help you:

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

I hope this could covers your requirements.

Good luck, Simeon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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