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.

4
  • Do you have the @DiscriminatorColumn? You need that so hibernate can instanciate the correct subclass. Commented Dec 16, 2021 at 22:20
  • I assume you haven't found stackoverflow.com/questions/25237664/… yet, please read up on it and see if it answers your question Commented Dec 17, 2021 at 8: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 Commented Dec 17, 2021 at 9: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. Commented Dec 17, 2021 at 21:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.