sadzag
sadzag

Reputation: 83

get records having admission date in a range

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

Answers (2)

Angular University
Angular University

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

Fabio
Fabio

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

Related Questions