title: javax.persistence.NoResultException: No entity found for query
body: <p>Before I posted this question, I already looked <a href="https://stackoverflow.com/questions/4848776/no-entity-found-for-query-exception">this</a>, but I couldn't get what I was looking for.</p>
  
  <p>I know that for the query I wrote there may exist only one row or none at all. So, there is not reason for me to use <code>getResultList()</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>String hql="from DrawUnusedBalance where unusedBalanceDate= :today";
  Query query=em.createQuery(hql);
  query.setParameter("today",new LocalDate());
  
  DrawUnusedBalance drawUnusedBalance= 
      (DrawUnusedBalance)query.getSingleResult();// we can have only a
                                                 // single datum per day
  //`System.out.println(drawUnusedBalance.toString());`
  </code></pre>
  
  <p>The problem is, if there is no row, it throws an exception, and if not it works fine. I know the problem but I am also looking for the best solution. </p>
  
  <p>What I wanted is, if there is no row in the DB I wanted to get a null object (instead of getting an exception) so I will insert a new data, if it is not null, I just want to update it.</p>
  
  <p>There is one way to handle this, which I believe is not the right way to do it. It is: I will have a try-catch block and if it throws an exception I can write to insert new data in to the DB on the catch block. But I believe there will be a better way.</p>