使用 findAll() 方法时如何避免休眠创建抽象对象 class?
How to avoid hibernate from creating object of abstract class when using findAll() method?
我有抽象的 class 员工,以及 3 个从员工扩展的具体的 class。
从 Employee
扩展而来的 类 例如 OfficeEmployee
当前是空的并且在 db 中也表示 table。这些具体 classes 的唯一目的是让 fk 引用 Employee
。比如创建了OfficeEmployee
,一个数据会保存在Employee
实体中,只有id会保存在OfficeEmployee
实体中。
这是我的员工class:
@Entity
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", nullable = false, unique = true)
private int employeeId;
@Column(name = "employee_name", nullable = false)
private String name;
@Column(name = "reason_for_deactivation", length = 255)
private String reasonForDeactivation = "";
@Column(name = "deleted", nullable = false)
private Boolean isDeleted = Boolean.FALSE;
}
我已经设法编写了保存、更新和删除特定员工的方法,但是当我想获取所有员工时我不能这样做,因为后台休眠正在尝试从员工创建对象 class我得到错误,因为 class 是抽象的。
这是获取所有员工的方法:
@Service
public class EmployeeServiceImpl {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> findAll() {
return employeeRepository.findAll();
}
}
我该如何解决这个问题?我愿意接受任何建议,包括更改我的架构。
最简单的解决方案是在 Employee
实体上添加 @DiscriminatorColumn
,在具体的 class 实体上添加 @DiscriminatorValue
。所以现在看起来像这样:
@Entity
@DiscriminatorColumn(name = "employee_type") // This is added
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", nullable = false, unique = true)
private int employeeId;
@Column(name = "employee_name", nullable = false)
private String name;
@Column(name = "reason_for_deactivation", length = 255)
private String reasonForDeactivation = "";
@Column(name = "deleted", nullable = false)
private Boolean isDeleted = Boolean.FALSE;
}
具体class:
@Entity
@Data
@DiscriminatorValue("Office_Employee") // This is added
public class OfficeEmployee extends Employee{
}
基本上,它会在名为 employee_type
的员工实体中添加新列,并且基于此将包含有关每个员工类型的信息,因此我现在可以获取所有员工。
我有抽象的 class 员工,以及 3 个从员工扩展的具体的 class。
从 Employee
扩展而来的 类 例如 OfficeEmployee
当前是空的并且在 db 中也表示 table。这些具体 classes 的唯一目的是让 fk 引用 Employee
。比如创建了OfficeEmployee
,一个数据会保存在Employee
实体中,只有id会保存在OfficeEmployee
实体中。
这是我的员工class:
@Entity
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", nullable = false, unique = true)
private int employeeId;
@Column(name = "employee_name", nullable = false)
private String name;
@Column(name = "reason_for_deactivation", length = 255)
private String reasonForDeactivation = "";
@Column(name = "deleted", nullable = false)
private Boolean isDeleted = Boolean.FALSE;
}
我已经设法编写了保存、更新和删除特定员工的方法,但是当我想获取所有员工时我不能这样做,因为后台休眠正在尝试从员工创建对象 class我得到错误,因为 class 是抽象的。
这是获取所有员工的方法:
@Service
public class EmployeeServiceImpl {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> findAll() {
return employeeRepository.findAll();
}
}
我该如何解决这个问题?我愿意接受任何建议,包括更改我的架构。
最简单的解决方案是在 Employee
实体上添加 @DiscriminatorColumn
,在具体的 class 实体上添加 @DiscriminatorValue
。所以现在看起来像这样:
@Entity
@DiscriminatorColumn(name = "employee_type") // This is added
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", nullable = false, unique = true)
private int employeeId;
@Column(name = "employee_name", nullable = false)
private String name;
@Column(name = "reason_for_deactivation", length = 255)
private String reasonForDeactivation = "";
@Column(name = "deleted", nullable = false)
private Boolean isDeleted = Boolean.FALSE;
}
具体class:
@Entity
@Data
@DiscriminatorValue("Office_Employee") // This is added
public class OfficeEmployee extends Employee{
}
基本上,它会在名为 employee_type
的员工实体中添加新列,并且基于此将包含有关每个员工类型的信息,因此我现在可以获取所有员工。