-->
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.  [ 8 posts ] 
Author Message
 Post subject: Facing problem while writing a polymorphic report query
PostPosted: Thu Oct 21, 2010 6:18 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
I've a scenario which I'm finding difficult to solve using a hql named query. Please if anybody can help.
I have Entities with all the relationships been defined using annotations. I've a case where I wanted to write a report query which fetches me all the details in a single DB interaction. The case is:

The relationships between entities are:
Person {
String key;
PhoneNumber phoneNumber;
}

abstract PhoneNumber {
String key;
String number;
}

MobileNumber extends PhoneNumber {
String countryCode;
}

LandLineNumber extends PhoneNumber {
String countryCode;
String areaCode;
}

Each one of them has a table in DB with proper constraints. I wanted to fetch a data transfer object like this:

select new com.DTO(p,
(case when m.key is not null then m.countryCode else ln.countryCode end),
(case when ln.key is not null then ln.areaCode else null end),
pn.number)
from Person p, PhoneNumber pn left join MobileNumber m on pn.key=m.key
left join LandLineNumber on pn.key=ln.key
where p.key = ?
and pn.key=p.phoneNumber.key

It gives error while parsing orm and making entity manager. Then I removed the colored section to see if it automatically adds the on while compiling to actual query, that as well didn't run.
Note: In abv case Person would always have a phoneNumber but the type number could differ.


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Tue Nov 02, 2010 10:14 am 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
No offense, but your class / table structure seems overly complicated for something so simple. Personally, I'd abandon inheritance entirely (especially a table per subclass approach) and consolidate all of the properties into a single class with a type property if you need to differentiate between mobile and home phone numbers:

Code:
class PhoneNumber {
  private String key;
  private PhoneNumberType type;
  private String countryCode;
  private String areaCode;
  private String number;
}


If the aim of your query is to select the phone number for a given person, then the HQL would simply be a matter of:

select phoneNumber from Person where key = :key

On a side note, you don't typically specify the join conditions in a query (with the exception of the join type). Assuming your classes are configured properly, Hibernate is aware of the underlying relationship that exists between the associations and will automatically generate the proper join SQL necessary to retrieve the data.


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Wed Nov 10, 2010 8:30 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
What I had given was just a simple example to my situation. Inheritance is defined in entities and each parent and its 7-8 child entities and all other linked entities have almost 50-60 attributes each and also the business scenario needs inheritance.

The problem I'm facing that I don't want to fetch whole of entity graph, I just want 1-2 fields from parent and 1 field from the child (i.e. attribute1 if child is child 1 or attribute 2 if child is child2).


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Wed Nov 10, 2010 12:15 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
It's a little strange... but this should work:

Code:
select parentAttribute1, parentAttribute2,
coalesce(
   (select childAttribute from Child1 c1 where c1.id = p.id),
   (select childAttribute from Child2 c2 where c2.id = p.id))
from Parent p


Assuming the problem that you're facing is that some of your subclasses share attributes of the same name...


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Thu Nov 11, 2010 5:42 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
surprise ya..

it gives NullPointerException while doing so.


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Thu Nov 11, 2010 11:05 am 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
Hmm... not sure why. I was able to run that myself against v3.5.6 without any issues. Posting the stack trace and HQL might help.


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Fri Nov 12, 2010 4:02 am 
Newbie

Joined: Thu Oct 21, 2010 5:15 am
Posts: 8
Thanks burroinquieto, now I'm able to successfully run my query, just using components helps. The problem I was facing may be because hibernate may not be supporting subselect within functions like 'coalesce', even in 'case when then end'.

Even I was facing problem trying to use mathematical operands like (+,-,..) with subselect on both side of operand. So now I'm doing the calculation on application side rather within query.

Thanks for all the help.


Top
 Profile  
 
 Post subject: Re: Facing problem while writing a polymorphic report query
PostPosted: Fri Nov 12, 2010 10:20 am 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
No problem. Glad you were able to get it working. As far as using a subselect within a coalesce, I didn't seem have any issues with the HQL that I posted. I tested in v3.5.6, so if you're using an earlier version then it's possible that may have something to do with the problems that you're seeing.


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