Hi Marten,
It seems to me the best way to handle this would be to map the contact info as a map collection. Here's what I tried.
The sql to build the example looks like this (HSQL in SqlTool):
Code:
-- drop the tables first
drop table contact_info;
drop table employee;
-- create the tables
create table employee (
employee_id identity
,employee_name varchar(30));
create table contact_info (
employee_id integer
,contact_type varchar(30)
,value varchar(30)
,foreign key (employee_id)
references employee (employee_id));
insert into employee values (null, 'mdeinum');
* employee_id ~
call identity();
insert into contact_info values (*{employee_id}, 'EMAIL', 'lala@lala.com');
insert into contact_info values (*{employee_id}, 'PHONE', '555.555.5555');
insert into contact_info values (*{employee_id}, 'NOTWANTED', 'blahblahblah');
insert into employee values (null, 'mojarrell');
* employee_id ~
call identity();
insert into contact_info values (*{employee_id}, 'EMAIL', 'other@email.com');
insert into contact_info values (*{employee_id}, 'PHONE', '111.111.1111');
insert into contact_info values (*{employee_id}, 'NOTWANTED', 'yadayadayadayada');
Here is the mapping for employee:
Code:
<class name="Employee" table="EMPLOYEE">
<id name="employeeId" column="EMPLOYEE_ID">
<generator class="native" />
</id>
<property name="employeeName" column="EMPLOYEE_NAME" />
<map
name="contactInfo"
table="CONTACT_INFO"
lazy="false">
<key column="EMPLOYEE_ID" />
<map-key column="CONTACT_TYPE" type="string" />
<element column="VALUE" type="string" />
</map>
</class>
The data manipulation code looks like this:
Code:
List employees = employeeDao.getAllEmployees();
it = employees.iterator();
while (it.hasNext()) {
Employee employee = (Employee) it.next();
lgr.info("Employee is: " + employee);
lgr.info("Phone Number is: " + employee.getContactInfo().get("PHONE"));
lgr.info("Email is: " + employee.getContactInfo().get("EMAIL"));
}
and the relavent output looks like this:
INFO Main - Employee is: Employee[0,mdeinum]
INFO Main - Phone Number is: 555.555.5555
INFO Main - Email is: lala@lala.com
INFO Main - Employee is: Employee[1,mojarrell]
INFO Main - Phone Number is: 111.111.1111
INFO Main - Email is: other@email.com
Hope this helps,