views:

32

answers:

1

I am currently working on a project which has Employee, Manager entities. At the very beginning, I took for granted that a manager must be an employee in the company, so they are the same person who have the same EmployeeID in payroll system. Based on this assumption, I let the Manager class extends Employee class.

Later, my client told me that sometimes some managers are not an employee in the company, but they still have employees report to them and these managers should also be in the system. The inheritance relationship between employee and manager is broken and now I have two independent classes with two tables (TAB_EMPLOYEE & TAB_MANAGER) in the database.

Both manager and employee can access to the system with an username and password. There is a class named User and the system should provide functions to create user account for managers and employees. In the previous situation, manager extends employee, I can create an user account for a manager and assign two roles (ROLE_EMPLOYEE, ROLE_MANAGER) to the user. But now, if an user account has already been created for an employee (this employee is also a manager, so besides a record in TAB_EMPLOYEE, there is a record in TAB_MANAGER), how can I create an user account for him as a manager? I know I can just assigned a ROLE_MANAGER to the existing user (so that the user has two roles: ROLE_EMPLOYEE, ROLE_MANAGER), but after the user logged in, I have to use the username or userid to load the referenced objects (Employee and/or Manager). In order to do this, I have to options:

Option 1: Add a userid column in both TAB_EMPLOYEE and TAB_MANAGER tables. Option 2: Add two columns ObjectType, ObjectId in TAB_USER, ObjectType contains EMPLOYEE or MANAGER, and ObjectId contains the related id.

I am not sure which option is better or is there any other options to do this?

+1  A: 

I would combine the tables so there is simply a staff table. Then I would add a unary relationship (pointer back to itself) to identify the manager. Lastly I would add a flag to identify internal staff (employees).

N8g
Thanks for you reply, N8g.In that case, I think I can let Manager subclass Employee and both of them may subclass class AbstractUser or implements an UserDetails interface if I want to use Spring Security.
Kevin Yang