8

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

Hazim
  • 805
  • 11
  • 24

3 Answers3

21

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.

Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23
  • Thanks, but what about the persistence.xml – Hazim Jun 12 '14 at 06:28
  • i meant what i have add in the persistence.xml, since i get "Class CommonFields is managed, but is not listed in the persistence.xml file" – Hazim Jun 12 '14 at 08:38
  • You don't have to add it in the persistence.xml, just the entities that inherit it. Annotating it with @MappedSuperclass and specifying the common files is enough – Gabriel Ruiu Jun 12 '14 at 08:42
  • i did, but what about the error "Class CommonFields is managed, but is not listed in the persistence.xml file" – Hazim Jun 12 '14 at 08:45
  • Hmm, I have never registered a MappedSuperclass in persistence.xml . Are you using Eclipse? In this SO post (http://stackoverflow.com/questions/2242637/class-model-address-is-listed-in-the-persistence-xml-file-but-not-mapped) the user says that you need to restart Eclipse or validate. I use intelliJ and don't have this issue – Gabriel Ruiu Jun 12 '14 at 08:48
  • Thanks, i need to add the CommonFields Class on the Persisitence.xml as the following link: http://www.javabeat.net/eclipselink-jpa-annotations-mappedsuperclass/ – Hazim Jun 15 '14 at 05:46
  • Question Why we do this with abstract class ?? . If this possible to do with class also – Jahongir Sabirov Apr 11 '20 at 10:08
  • @JahongirSabirov because the OOP concept behind fits exactly to the data modelling requirement. A series of data objects define one or more fields which are exactly the same, and in order to reduce code and logic duplication, a dedicated data object is defined to contain the fields which all of the other data objects used. A classic inheritance use-case – Gabriel Ruiu Apr 14 '20 at 15:09
0

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/

paul
  • 12,873
  • 23
  • 91
  • 153
-1

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

EclipseLink / JPA Annotations – @MappedSuperclass

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hazim
  • 805
  • 11
  • 24
  • Will this solution not generate a table for the `CommonFields` entity (which you probably do not want, as you will probably will have some Entity Inheritance headache)? – V G Jun 12 '14 at 09:36
  • Well, Hazim: you added `@MappedSuperclas` (which is what Gabriel's answer says). SO your answer should be actually a comment to his answer. – V G Jun 12 '14 at 12:11
  • Thanks Andrei I, because i use the eclipse, and the if i added only it will not working fine so i have to add the class on the persistence.xml also to be working fine. – Hazim Jun 15 '14 at 05:45