What is the best way to implement user hierarchy (multi level)

I have this scenario that one user is actually a department manager and he is standing in the head of a pyramid.
The pyramid depth level is unknown, can be one, or can be 4.

I want to create a user, define his manager.
End of the day, in the reports I want to see an aggregation of a user and the performance of his employees/users.

What are the best practices in this case? is done in a demo project?

Thanks .

I cannot remember an example for it, but I would create a new entity called Department with a field User manager and List<User> employees which has a @ManyToMany cardinality.
After that you’ll be able to find all departments where the current user is a manager and all employees of this department.
Will that work for your case?

No, I need the full hierarchy… and find the path.

User A
 -> User B
   -> User C

I need to know that User is under A and B.

  1. Each user can have only one manager.
  2. You need an API that will return:
    a) All managers for the user (For C A and B will be returned)
    b) All subordinates of the user (for A all subordinates of A + all subordinates of B + all subordinates of C will be returned)

Are the statements above correct?


  1. yes, but remember I can have “parent” and a grand parent
  2. yes. maybe a way to query it efficiently.

Here is a sample project: org-structure-sample.zip (90.9 KB)

A new entity Employee with a one-to-one association to User is created. The Employee has a reference to a parent employee and subordinates.

@Table(name = "SAMPLE_EMPLOYEE")
@Entity(name = "sample$Employee")
public class Employee extends StandardEntity {

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID")
    protected User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MANAGER_ID")
    protected Employee manager;

    @OneToMany(mappedBy = "manager")
    protected List<Employee> subordinates;

    public List<Employee> getSubordinates() {
        return subordinates;

    public void setSubordinates(List<Employee> subordinates) {
        this.subordinates = subordinates;

    public Employee getManager() {
        return manager;

    public void setManager(Employee manager) {
        this.manager = manager;

    public User getUser() {
        return user;

    public void setUser(User user) {
        this.user = user;

An entity listener that automatically creates a related employee when a new user is created:

 * Listener automatically creates an employee when the user is created
public class MyUserEntityListener implements BeforeInsertEntityListener<User> {

    protected Metadata metadata;

    public void onBeforeInsert(User user, EntityManager entityManager) {
        Employee employee = metadata.create(Employee.class);

A middleware service that returns all subordinates and managers for the selected employee.

public class OrgStructureServiceBean implements OrgStructureService {

    protected DataManager dataManager;

    protected EntityStates entityStates;

    public List<Employee> loadAllManagers(Employee employee) {
        List<Employee> managers = new ArrayList<>();
        if (!entityStates.isLoadedWithView(employee, "employee-with-manager")) {
            employee = dataManager.reload(employee, "employee-with-manager");
        while (employee.getManager() != null) {
            employee = dataManager.reload(employee.getManager(), "employee-with-manager");
        return managers;

    public List<Employee> loadAllSubordinates(Employee manager) {
        return loadAllSubordinates(Collections.singletonList(manager));

    protected List<Employee> loadAllSubordinates(List<Employee> managers) {
        List<Employee> subordinates = dataManager.load(Employee.class)
                .query("select s from sample$Employee e join e.subordinates s where e.id in :managers")
                .parameter("managers", managers)

        if (!subordinates.isEmpty()) {

        return subordinates;

Each employee returned by the service has a user property, so you can easily get a User entity from it.
You can test the service using the Org Structure Test Screen that is available from the menu.