-->
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.  [ 3 posts ] 
Author Message
 Post subject: Transfer SQL query to Hibernate Criteria
PostPosted: Sun Dec 09, 2007 12:48 pm 
Newbie

Joined: Sun Dec 09, 2007 12:33 pm
Posts: 2
Hibernate version:
Hibernate 3.2

Name and version of the database you are using:
PostgreSQL 8.3

Hi

I have this sql query:

Code:
SELECT d.title, pod.definition
FROM
Demand d
LEFT OUTER JOIN price_of_demand pod
ON pod.demand_fxid = d.demand_pxid AND pod.display IS TRUE


I need transfer this query to Criteria. Now I have:

Code:
Criteria criteria = HibernateUtil.currentSession().createCriteria(Demand.class);
criteria.createAlias("priceOfDemands", "pod", JoinFragment.LEFT_OUTER_JOIN);   
ProjectionList projections = Projections.projectionList();   
projections.add(Projections.property("demandPxid"));
projections.add(Projections.property("pod.definition"));
criteria.setProjection(projections);


But I don't know how in Criteria define this part:
Code:
pod.display IS TRUE


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 09, 2007 3:59 pm 
Newbie

Joined: Fri Mar 30, 2007 5:23 pm
Posts: 16
Location: New York, NY
Assuming that in your Hibernate mapping you have pod.display mapped as a boolean, the following code should work:

Code:
criteria.add(Restrictions.eq("pod.display", Boolean.TRUE))

I've never used aliases in Criteria but I believe that the alias you defined would allow you to make that statement. Alternatively (here's how I would do it if it were my code - this isn't necessarily better):

Code:
Criteria podCriteria = criteria.createCriteria("priceOfDemands");
podCriteria.add(Restrictions.eq("display", Boolean.TRUE));

Hope that helps!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 5:07 pm 
Newbie

Joined: Sun Dec 09, 2007 12:33 pm
Posts: 2
Thank you for your help.

1)
But if I use
Code:
criteria.add(Restrictions.eq("pod.display", Boolean.TRUE))

that is the same as
Code:
SELECT d.title, pod.definition
FROM
Demand d
LEFT OUTER JOIN price_of_demand pod
ON pod.demand_fxid = d.demand_pxid
WHERE pod.display IS TRUE

but it isn't the same as
Code:
SELECT d.title, pod.definition
FROM
Demand d
LEFT OUTER JOIN price_of_demand pod
ON pod.demand_fxid = d.demand_pxid
AND pod.display IS TRUE


2)
If I use
Code:
Criteria podCriteria = criteria.createCriteria("priceOfDemands");
podCriteria.add(Restrictions.eq("display", Boolean.TRUE));


that I can't use
Code:
projections.add(Projections.property("pod.definition"));
criteria.setProjection(projections);

because I have 2 different criteria (criteria, podCriteria) and how i use projections to 2 criterias?


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