-->
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.  [ 4 posts ] 
Author Message
 Post subject: Polymorphic query: different subclass properties
PostPosted: Mon Oct 30, 2006 2:31 pm 
Newbie

Joined: Mon Oct 04, 2004 5:09 pm
Posts: 4
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>


Top
 Profile  
 
 Post subject: Any solution?
PostPosted: Mon Feb 12, 2007 9:54 am 
Newbie

Joined: Wed Sep 06, 2006 6:08 am
Posts: 11
Location: Rome, Italy
Hi, did you figure out a clean way of doing this? I'm having a similar problem.

Ken


Top
 Profile  
 
 Post subject: Polymorphic query: different subclass properties
PostPosted: Mon Jun 18, 2007 8:02 am 
Newbie

Joined: Mon Oct 04, 2004 5:09 pm
Posts: 4
Ken, to solve this problem, I added an abstract method to Party called getPartyName. Hibernate retrieves the appropriate Party instance and that implementation returns the appropriate "name" for either Person or Organizaiton.

Not really the solution i was looking for, but it worked for this particular issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 10:19 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
You can use the property partyName in a hql query although it isn't mapped? Or did you add something to your mapping file in order to be able to use the property partyName in your query?

Thanks for your answer
Hoeft


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.