JPA实体扩展类包含@Id。

8
我有许多实体类,都包含主键Id。我想创建一个抽象类,将所有共同字段放在其中,并允许所有类继承该抽象类,如下所示:
public abstract class CommonFields{
    @Id
@Column(name = "ID")
private long id;

public void setId(long id) {
    this.id = id;
}

public long getId() {
    return id;
}
}

@Entity
    @Table
    public class B extends CommonFields{
    String carModel;
}




@Entity
    @Table
    public class A extends CommonFields{
    String name;
}

感谢大家

是的,可以的,id 是继承的,所有其他字段也是继承的。 - kostja
3个回答

21

您可以使用 @MappedSupperclass 注释类来标注常见字段。

@MappedSuperclass
public abstract class CommonFields{
    @Id
    @Column(name = "ID")
    private long id;

    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }
}

来自@MappedSuperclass文档:

指定一个类,其映射信息应用于继承自它的实体。映射超类没有单独为其定义表。


谢谢,但是 persistence.xml 怎么办? - Hazim
我指的是我在persistence.xml中添加的内容,因为我收到了“类CommonFields已被管理,但未在persistence.xml文件中列出”的错误提示。 - Hazim
你不必在persistence.xml中添加它,只需继承它的实体。使用@MappedSuperclass注释并指定公共文件即可。 - Gabriel Ruiu
哦,我从未在persistence.xml中注册过MappedSuperclass。你在使用Eclipse吗?在这个SO帖子(https://dev59.com/DHE95IYBdhLWcg3wlvCz)中,用户说你需要重新启动Eclipse或验证。我使用的是IntelliJ,没有这个问题。 - Gabriel Ruiu
谢谢,我需要在Persisitence.xml中添加CommonFields类,如下链接所示:http://www.javabeat.net/eclipselink-jpa-annotations-mappedsuperclass/ - Hazim
显示剩余3条评论

0
当然可以。使用Hibernate,你可以使用三种不同的实现。
使用判别器:通过@DiscriminatorValue来确定实体类型。在这种情况下,两个实体共享同一个表格。
        @Entity
    @Table(name = "PERSON")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(
        name="discriminator",
        discriminatorType=DiscriminatorType.STRING
    )
    @DiscriminatorValue(value="P")
    public abstract class Person {

        @Id
        @GeneratedValue
        @Column(name = "PERSON_ID")
        private Long personId;

        @Column(name = "FIRSTNAME")
        private String firstname;

        @Column(name = "LASTNAME")
        private String lastname;

        // Constructors and Getter/Setter methods, 
    }


  @Entity
  @Table(name="PERSON")
  @DiscriminatorValue("E")
  public class Employee extends Person {

      @Column(name="joining_date")
      private Date joiningDate;

      @Column(name="department_name")
      private String departmentName;

      // Constructors and Getter/Setter methods, 
  }

或者您可以针对每个子类使用一个表:

    @Entity
    @Table(name = "PERSON")
    @Inheritance(strategy=InheritanceType.JOINED)
    public abstract class Person {

        @Id
        @GeneratedValue
        @Column(name = "PERSON_ID")
        private Long personId;

        @Column(name = "FIRSTNAME")
        private String firstname;

        @Column(name = "LASTNAME")
        private String lastname;

        public Person() {

        }

        public Person(String firstname, String lastname) {
            this.firstname = firstname;
            this.lastname = lastname;
        }

        // Getter and Setter methods, 
    }

      @Entity
      @Table(name="EMPLOYEE")
      @PrimaryKeyJoinColumn(name="PERSON_ID")
      public class Employee extends Person {

          @Column(name="joining_date")
          private Date joiningDate;

          @Column(name="department_name")
          private String departmentName;

          public Employee() {
          }

          public Employee(String firstname, String lastname, String departmentName, Date           joiningDate) {

              super(firstname, lastname);

              this.departmentName = departmentName;
              this.joiningDate = joiningDate;
          }

          // Getter and Setter methods, 
      }

你可以在这里找到其余的细节 http://viralpatel.net/blogs/hibernate-inheritance-table-per-subclass-annotation-xml-mapping/


-1

这个解决方案不会为 CommonFields 实体生成一个表格(你可能不想要这个,因为你可能会遇到一些实体继承的问题)。 - V G
好的,Hazim:你添加了@MappedSuperclass(这就是Gabriel的答案所说的)。因此,你的答案实际上应该是对他答案的评论。 - V G
感谢Andrei I,因为我使用Eclipse,如果我只添加它,它将无法正常工作,所以我还必须在persistence.xml中添加类才能正常工作。 - Hazim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接