0

I have abstract class Employee, and 3 concrete class that extends from employee. Classes who extends from Employee for example OfficeEmployee are currently empty and also represents table in db. Only purpose of these concrete classes is to have fk reference to Employee. For example if OfficeEmployee is created, a data will be saved in Employee entity and only id will be saved in OfficeEmployee entity.

This is my Employee 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;
}

I have managed to write methods for saving,updating and deleting specific employee, but when i want to fetch all Employees i can't do that because hibernate in background is trying to create object from Employee class and i get error because that class is abstract.

This is method for getting all Employees:

@Service
public class EmployeeServiceImpl {

@Autowired
private EmployeeRepository employeeRepository;

  public List<Employee> findAll() {
    return employeeRepository.findAll();

 }
}

How can i solve this problem? I am open for any advice including changing my architecture.

N J
  • 33
  • 4
  • Do you have the @DiscriminatorColumn? You need that so hibernate can instanciate the correct subclass. – Martin Frey Dec 16 '21 at 22:20
  • I assume you haven't found https://stackoverflow.com/questions/25237664/use-abstract-super-class-as-parameter-for-spring-data-repository yet, please read up on it and see if it answers your question – PaulD Dec 17 '21 at 08:56
  • @MartinFrey I was trying to avoid adding new column in Employee, but i don't have a choice at this point. Thank's though – N J Dec 17 '21 at 09:17
  • If it‘s just about referencing an employee with an office, and other references without additional attributes you can make employee a concrete class and use manytomany properties. – Martin Frey Dec 17 '21 at 21:39

1 Answers1

1

The simplest solution is to add @DiscriminatorColumn on Employee entity and @DiscriminatorValue on concrete classes. So now it looks like this:

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

And concrete class:

@Entity
@Data
@DiscriminatorValue("Office_Employee")  // This is added
public class OfficeEmployee extends Employee{

}

Basically it will add new column in Employee entity called employee_type and based on that will have information on type of each employee, therefore i can now fetch all Employees.

N J
  • 33
  • 4