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: [Hibernate/MySQL] : Natif SQL queries ???
PostPosted: Wed Sep 15, 2004 9:39 am 
Regular
Regular

Joined: Sat May 15, 2004 4:27 am
Posts: 79
Hi all,

who can tell me how can i translate the current following query in Hibernate :

SELECT *
FROM contracts
WHERE purchase_date <= DATE_FORMAT(NOW(),'%Y-%m-%d')
AND end_date >= DATE_FORMAT(NOW(),'%Y-%m-%d')

Thanks

-------------------
Sebastien


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 11:28 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Hello smayemba,

Although Hibernate QL looks similar to SQL it is not. The selection depends on the type of the object you use.
My first intuition would say (java coded):
Code:
String hql=
"Select contracts from Contract as contracts Where
contracts.purchasedate<=:purchasedate and
contracts.enddate>=:enddate;";

Query q = Session.createQuery(hql);
q.setParameter("purchasedate",Calendar.getInstance());
q.setParameter("enddate",Calendar.getInstance());

List result = q.list();

This is out of my head, so there could be some mistakes in it. Hope this helps.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 11:48 am 
Regular
Regular

Joined: Sat May 15, 2004 4:27 am
Posts: 79
Thank u Dencel,

i did the same query than you :

Code:
SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_FORMAT);
Calendar currentCal = new GregorianCalendar();
currentCal.setTime(new java.util.Date());
String datetime = sdf.format(currentCal.getTime());       

Query q = session.createQuery("FROM Contract as co WHERE :mydate >= co.purchaseDate and :mydate <= co.endDate");
q.setParameter("mydate", datetime);
java.util.List contracts = q.list();


---------------
Sebastien


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.