title: Handling null parameters in JPQL
body: <p>I just realized that JPQL where clause does not work as I expected when any of the parameters is null. 
  For instance this simple JPQL query</p>
  
  <pre><code>SELECT en FROM Entity en WHERE en.name = :name
  </code></pre>
  
  <p>does not return any results when the <code>name</code> parameter is null even if there are entities with name set to null in the database.</p>
  
  <p>In that case Hibernate executes SQL with <code>WHERE entity.NAME = null</code>. Obviously this is not handled by databases because standard defines <code>IS NULL</code> for null comparisons instead(see <a href="https://stackoverflow.com/questions/9581745/sql-is-null-and-null#">SQL is null and = null</a>). JPQL has <code>IS NULL</code> operator as well(<a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbvi" rel="nofollow noreferrer">http://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbvi</a>) that behaves exactly like in SQL(<a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbvr" rel="nofollow noreferrer">http://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbvr</a>).</p>
  
  <p>I thought this is a frequent case but a quick search did not give my any interesting results. </p>
  
  <p>So, is there any way to include null values in the query?
  So far I came up with the following:</p>
  
  <pre><code>SELECT en FROM Entity en WHERE (:name IS NULL AND en.name IS NULL) OR en.name = :name
  </code></pre>
  
  <p>It works fine but it does not look elegant, especially in larger queries.</p>
  
  <p>Bonus question: Why does JPQL mimic this weird aspect of SQL?</p>