0

For example I have a named query:

@NamedQuery(name = "Students", query = "SELECT s FROM Student WHERE s.active = 'Y' OR s.name= :name)

Sometimes I wanna set the 'name' parameter with a value and sometimes not. Does it cause an error when I don't set it?

Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
kamaci
  • 69,365
  • 67
  • 217
  • 352
  • related: http://stackoverflow.com/questions/2444603/optional-parameters-with-named-query-in-hibernate – Bozho Dec 22 '10 at 12:33

2 Answers2

1

You have to set it always. Otherwise - create two queries - one with param, and one without. You can place the common part in a constant (static final)

Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
0

I found a solution like this:

if (my test criteria){
     query.setParameter("name", "name of a student");
  }  else {
     query.setParameter("name", " ");
  }

I am setting the name parameter with whitespace character so it works.

kamaci
  • 69,365
  • 67
  • 217
  • 352