简体   繁体   中英

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!

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.

It's hard to say something without model, but you can try to use alias to join your three tables. There are javadoc and some examples available.

To get unassigned tasks you can add NotEqual restriction @VytautasAlkimavičius mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM