Reputation: 83
Using Hibernate I want to get clients ID from Contract table where the admission date is in a range( between two dates).
List clients = session.createQuery("from Contract contract where contrat.datesouscription BETWEEN "+ begindate+" and "+endate).list();
my query doesn't work. Thanks
Upvotes: 1
Views: 72
Reputation: 43087
Try this query with parameters, as buiding queries via concatenation is not recommended due to security reasons (opens up the code to SQL injection attacks):
String hql = "from Contract contract where contrat.datesouscription BETWEEN :beginDate and :endDate";
List result = session.createQuery(hql)
.setParameter("beginDate", begindate)
.setParameter("endDate", enddate)
.list();
Have a look at these examples.
Upvotes: 1
Reputation: 23500
You need quotes around your dates
List clients = session.createQuery("from Contract contract where contrat.datesouscription BETWEEN '"+begindate+"' and '"+endate+"'").list();
Upvotes: 1