Ignoring a @MappedSuperclass for not extended class JPA

If for example I have two class FullTimeEmployee and PartTimeEmployee . FullTimeEmployee has to extend Employee table and to access that properties by using @MappedSuperclass but PartTimeEmployee class does not required any extend but it throws JPA error if not extended all the other classes.

@MappedSuperclass
public class Employee {
      @Id
      @GeneratedValue
      private long id;
      private String name;
        .............
}

@Entity
public class FullTimeEmployee extends Employee {
  private int salary;
    .............
}

I don't want to extended PartTimeEmployee class with employee @MappedSuperclass does not allow me to do that

@Entity
public class PartTimeEmployee {
  private int hourlyRate;
    .............
}

Upvotes: 0

Views: 442

Answers (1)

pirho
pirho

Reputation: 12275

You should reconsider your desing and entity naming. If your PartTimeEmployee does not extend Employee you need at least the id so:

@Entity
public class PartTimeEmployee {
    @Id
    @GeneratedValue
    private long id;
    private int hourlyRate;
    . . .
}

OR you could also create a bit more cleverdesign and a mapped superclass like:

@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue
    private long id;

Thne your Employee could extend it and inherit id:

@MappedSuperclass
public class Employee extends BaseEntity {
    private String name;
    . . .
}

Your FullTimeEmployee could be as it is and PartTimeEmployee could extend BaseEntity to inherit id:

@Entity
public class PartTimeEmployee extends BaseEntity {
    private int hourlyRate;
. . .
}

Upvotes: 1

Related Questions