简体   繁体   English

对于未扩展的类JPA忽略@MappedSuperclass

[英]Ignoring a @MappedSuperclass for not extended class JPA

If for example I have two class FullTimeEmployee and PartTimeEmployee . 举例来说,如果我有两个类FullTimeEmployeePartTimeEmployee。 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. FullTimeEmployee必须扩展Employee表并使用@MappedSuperclass访问该属性,但是PartTimeEmployee类不需要任何扩展,但是如果不扩展所有其他类,它将抛出JPA错误

@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 我不想用员工@MappedSuperclass扩展PartTimeEmployee类,不允许我这样做

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

You should reconsider your desing and entity naming. 您应该重新考虑您的设计和实体命名。 If your PartTimeEmployee does not extend Employee you need at least the id so: 如果您的PartTimeEmployee不扩展Employee ,则至少需要id因此:

@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 : 您的Employee可以扩展它并继承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 : 您的FullTimeEmployee可以是原样, PartTimeEmployee可以将BaseEntity扩展为继承id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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