You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Andrew Cooke <an...@acooke.org> on 2008/06/24 10:09:49 UTC

1:1 with Duplicates

Hi,

(I'm unsure how to describe this, so am having problems searching for an answer.
 I have a solution (described below), but wonder if there's a simpler way.)

I have two tables, related by an inner join, something like this:

table employees:
text email
...
integer dept_id

table departments:
integer dept_id
text department_name
...

and two classes:

class Employee:
private Department my_department
private String email
...

class Department:
private String department_name
...

*** But many employees belong to one department ***

My problem is how to avoid duplicating Department instances when I select a
group of employees.

When I tried a simple map (with a nested map for Department) I got a new
Department instance for each employee.  When I tried using groupBy in the
nested map I no longer got duplicate Departments, but the department was 
only set on the first employee of each department.

My current solution is to construct an intermediate object that contains 
an Employee and a dept_id and then iterate through in Java, selecting for 
the Department using the dept_id.  Because I am using caching, iBatis then 
returns a cached Department when appropriate and I can insert the Department
in the Employee "by hand".

However, I wondered if there was a simpler way.

I hope the above makes sense,
Andrew