-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping results to simple properties
PostPosted: Wed Jun 28, 2006 5:21 am 
Newbie

Joined: Wed Jun 28, 2006 5:06 am
Posts: 4
Location: The Netherlands
Hi,

I seem to be stuck with a hibernate mapping issue. Tried several things but none of them seem to work.

Currently we migrated to hibernate 3.1.3 to be able to use the <join> tag which we thought would solve our problem. However the result we got was not what we expected.

So what do I want to achieve.

We have 2 tables 1 employee table and 1 table containing all of the contact info.

EMPLOYEE
------------
ID
NAME
REST OF FIELDS

CONTACT_INFO
------------------
EMPLOYEE_ID
CONTACT_TYPE
VALUE

The employee has 2 fields (phone and email) which I want to map to 2 (optional) results from the contact_info. Contact_info holds more properties than those 2 so I want only the ones of type EMAIL and PHONE. And map those to 2 string values in my employee class. Probleem is that the properties can be changed (So using a subselect in mapping files probably won't do it).

However when we use the <join> tag we get 2 rows for every employee, I would like to have 1 row, containing the value (or null). However I seem to be at a loss how to achieve this in the mapping file...

The SQL query for selecting should look somewhat similair to this:

SELECT emp.id, emp.name,
(select VALUE from CONTACT_INFO ci where type='PHONE' and ci.employee_id=employee.id) as phone,
(select VALUE from CONTACT_INFO ci where type='EMAIL' and ci.employee_id=employee.id) as email
from employee emp
where emp.id=21950


Currently don't have the mapping file add hand, will post what we have (pseudo like) when I do.

Marten


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 10:46 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
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,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.