Greetings,
I have the following model.
Party
- id
Person extends Party
- firstName
- lastName
Organization extends Party
- name
Person and Organization are "joined" subclasses (table per subclass strategy) of Party.
The schema reflects the model where both Person and Organization tables share
the Party primary key and extend the table with their respective unique attributes.
What I would like to do in HQL is to retrieve a list of all Party names.
The following is the SQL I would like to emmulate.
Code:
select case when p.partyTypeId = 1 then concat_ws(' ', person.firstName, person.lastName) else org.name end as name
from Party p
left outer join Person person on p.id = person.id
left outer join Organization org on p.id = org.id
I'm not sure how to accomplish this in HQL. If there were mappings from Party to Person and Organization,
then I could do something like
Code:
select case when p.class = Person then concat(person.firstName, ' ', person.lastName) else org.name end as name
from Party p
left join p.person person
left join p.organization org
but there is no such mapping. The only way I can figure out how to accomplish
this is to provide a one-to-one mapping from Party to both Person and Organization but
this seems like a hack just to get HQL to do what I want.
I'm sure I've missed something simple. Any help or suggestions is appreciated.
Hibernate version: 3.2.0
Name and version of the database you are using:MySQL 4.1.11
Mapping documents:Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class abstract="true" name="com.company.common.domain.party.Party">
<id name="id">
<generator class="native"/>
</id>
<many-to-one column="partyTypeId" class="com.company.common.domain.party.PartyType" not-null="true" name="partyType"/>
<joined-subclass name="com.company.common.domain.party.Organization" table="Organization">
<key column="id"/>
<property name="name"/>
</joined-subclass>
<joined-subclass name="com.company.common.domain.party.Person" table="Person">
<key column="id"/>
<component name="personName" class="com.company.common.domain.party.PersonName">
<property name="last" column="lastName"/>
<property name="first" column="firstName"/>
</component>
</joined-subclass>
</class>
</hibernate-mapping>