user10591831
user10591831

Reputation:

HQL currentdate

I'm trying to create queries in HQL using the currentdate() function to compare two dates.

List<Rel_Utenti_Commesse> commesseAttive = new ArrayList<Rel_Utenti_Commesse>();        

commesseAttive = session.createQuery("from Rel_Utenti_Commesse where utenti = '" + utenteSessione + "' and data_fine_commessa > current_date() or data_fine_commessa is null").list();

for(int i = 0; i < commesseAttive.size(); i++)                   
    System.out.println(commesseAttive.get(i).getCommessa().getNome_commessa());

return commesseAttive;

But although I have in the database (MySQL) an instance with a data_fine_commessa with 2019-02-01, he ignores it.

Why?

Upvotes: 0

Views: 185

Answers (1)

Jens
Jens

Reputation: 69440

You miss the braces arrond the or part:

commesseAttive = session.createQuery("from Rel_Utenti_Commesse where utenti = '" + utenteSessione + "' and (data_fine_commessa > current_date() or data_fine_commessa is null)").list();

That's why first is AND evaluated and than the OR

@TimBiegeleisen suggested, use parametrized queries:

Query query = session.createQuery("from Rel_Utenti_Commesse where utenti = :utenteSessione and (data_fine_commessa > current_date() or data_fine_commessa is null)")
query.setParameter("utenteSessione", utenteSessione)
List result = query.list()

Upvotes: 1

Related Questions