-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to retrieve projection and object using Criteria Query
PostPosted: Mon Nov 19, 2007 8:09 pm 
Newbie

Joined: Wed Nov 14, 2007 8:19 pm
Posts: 5
Hibernate version:3.30GA


I need to do something similar to the following HQL, but need to use the Criteria API because I'm doing complex query assembly on the fly:

"select p , p.field1 * p.field2 / p.field3 as calc1 from Person p order by calc1 desc "


I'm having trouble using the criteria API. For example when I do the following:

Code:
DetachedCriteria query= DetachedCriteria.forClass(Person.class, "p");
Projection proj= Projections.sqlProjection("{alias}.field1 * {alias}.field2 / {alias}.field3", new String[] {"calc1"}, new Type[] {Hibernate.DOUBLE});
query.setProjection(Projections.projectionList().add(Projections.alias(Projections.property("p"),"person")).add(proj));
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List<Map> results =  query.getExecutableCriteria(sess).setMaxResults(10).list();

for(Map<String,Object> map: results){
         Person result = (Person)map.get("person");
         if(map.containsKey("calc1")){
            foo.field= (Double)map.get("calc1");
         }

I get a property exception stating that Hibernate cannot resolve "p". It is as if the alias "p" does not register or have context under a projection context like above.
if I remove the alias is produces the problem, such as :
Code:
query.setProjection(Projections.projectionList().add(Projections.property("p")).add(proj));


Note, if I remove the "p" property all together from the projection, the calculation an map binding works as expected.

I'm I on the right track here, or is another approach required? It is important that I perform the calculation in the DBMS because of the order by clause and the large set size. It seems that this would be a simple thing to accomplish, but there are no examples... desperate help is needed.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 8:44 pm 
Newbie

Joined: Wed Nov 14, 2007 8:19 pm
Posts: 5
Bump...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 9:51 pm 
Newbie

Joined: Wed Nov 14, 2007 8:19 pm
Posts: 5
Bump...

Can anybody assist with this issue. Has anyone else performed a projection along with the root entity object itself? If not, what workarounds can you recommend?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 6:06 pm 
Newbie

Joined: Wed Nov 14, 2007 8:19 pm
Posts: 5
Bump...

One last time, folks. If anyone can comment on this problem, I would appreciate it. Is there any other forum/venue/etc. where I may be able to find assistance? I also forgot to mention that I'm using Annotations to denote persistence.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 8:26 am 
Newbie

Joined: Tue May 10, 2005 6:15 am
Posts: 4
I have the same problem. Any solution on that?


Top
 Profile  
 
 Post subject: Re: How to retrieve projection and object using Criteria Query
PostPosted: Thu Mar 11, 2010 12:18 pm 
Newbie

Joined: Thu Mar 11, 2010 12:05 pm
Posts: 1
Does anybody resolve this problem ? I have similar (with add object mapping into Projection)


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