简体   繁体   中英

JPA entity extends class contain @Id

i have entities classes all contains id as primary key, can i create abstract class which contains all common fields and allow all classes extends this class as the following :

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

Thank You All

You can annotate the class with the common fields with @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;
    }
}

From the @MappedSuperclass doc:

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

Sure you can. With Hibernate, you can use three diffewrents implementations.

Use discriminators: Which @DiscriminatorValue Determine the entity type. This case both entities share same table

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

Or you can use a table per subclass:

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

You can find the rest of the details here http://viralpatel.net/blogs/hibernate-inheritance-table-per-subclass-annotation-xml-mapping/

Just add the CommonFields class as entity in the persistence.xml file.

EclipseLink / JPA Annotations – @MappedSuperclass

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