Search code examples
javahibernatehibernate-criteria

Compare two tables of the same objects and select the different ones


I have a joint table named EmployeeTask join by Employee and Task.

Relationship: Employee (ONE)----(MANY) EmployeeTask (MANY)----(ONE) Task.

So now I have an employee, and I use his employee ID to find all the employeeTask this employee has been assigned to, and subsequently find all the Task correspond to each employeeTask into a table.

Next, I get all the Task i have in the database into another table.

Finally, I want to find out the list of tasks that has not been assigned to this employee.

Tools I am using is hibernate criteria and the language is JAVA

Can anyone help, Please.

---EDIT---

This piece of code will give me all the Task I have for the given employee:

ArrayList<EmployeeTask> employeeTasks = EmployeeTaskDAO.getEmployeeTasksByEmployee(employee);
JSONArray tasksArr = new JSONArray();
for(EmployeeTask et : employeeTasks){
    Task t = TaskDAO.getTaskById(et.getTask().getTaskId());
    tasksArr.add(t.toJson());
}

The method to getEmployeeTasksByEmployee

public static ArrayList<EmployeeTask> getEmployeeTasksByEmployee(Employee employee){
    ArrayList<EmployeeTask> employeeTasks = new ArrayList<EmployeeTask>();
    if(employee != null){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EmployeeTask.class);
        detachedCriteria.add(Restrictions.eq(Key.EMPLOYEE, employee));
        List<Object> list = HibernateUtil.detachedCriteriaReturnList(detachedCriteria);
        for(Object o : list){
            employeeTasks.add((EmployeeTask) o);
        }
    }
    return employeeTasks;
}

The way of getting all the Task is quite similar to the above code sample.

Now I am literally lost in the way to find the rest of the tasks that are not been assign to this employee yet. What is the way I shall approach? Any help? Thanks!


Solution

  • Instead of searching for the differences in two tables. I decided to store two different object array in javascript, and compare the two array to take out the uncommon ones.