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
  • 588,226
  • 146
  • 1,060
  • 1,140
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 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
  • 588,226
  • 146
  • 1,060
  • 1,140
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
  • 72,915
  • 69
  • 228
  • 366