-->
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.  [ 5 posts ] 
Author Message
 Post subject: Dynamic instantiation with entity parameter
PostPosted: Wed Dec 22, 2004 1:06 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Hi all,

Wondering if someone can help. I have three classes: ClassA, ClassB, and ClassC. ClassB has two one-to-many relationships: one with ClassA and one with ClassC. I'm trying to execute HQL that resembles the following:

Code:
select new ClassA( c.objectB, avg(c.someValue) )
from ClassC as c
group by ( c.objectB )


In the query, objectB is of type ClassB.

Now, ClassA has a constructor defined as: ClassA(ClassB, double), which I'm trying to call with this query. Unfortunately, it fails with a SQL exception because the generated SQL SELECT contains all of the properties of ClassB, but the GROUP BY contains only the ID of ClassB.

My question is: How can I get this query to work? If I change the SELECT clause to select only the ID of objectB, then it still doesn't work because the constructor would need to take the ID as input rather than the entity object itself. (I want the object itself or at least a proxy.)

I'm using Hibernate 2.1.6. Any ideas?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you need to list all the properties on the object when doing this AFAIK

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 2:19 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
max wrote:
you need to list all the properties on the object when doing this AFAIK


You mean in the GROUP BY clause? Ugh. That would not be fun because there are a lot of properties.

I guess I could remove ClassB from the constructor and have the query return tuples of the ClassA object and the ID of objectB. Then in my DAO, I could load and set the instacnes of objectB before returning a list of ClassA objects to the caller.

Is there an easier way?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 3:13 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Well, it seems my idea doesn't work. Apparently, if you use dynamic instantiation in a SELECT clause, the clause cannot contain any other items. I traced QueryHelper.renderSQL() and found that it looks for a constructor for all of the items in the SELECT clause, regardless of whether they are contained within the argument list of new ClassA(...). Looks like a bug to me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 3:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no as far as i know...

_________________
Max
Don't forget to rate


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