title: Query returning multiple objects when only one is expected
body: <p>I have a simple table:</p>
  
  <pre><code>+-------------------+--------------+------+-----+---------+-------+
  | Field             | Type         | Null | Key | Default | Extra |
  +-------------------+--------------+------+-----+---------+-------+
  | ID                | bigint(20)   | NO   | PRI | NULL    |       |
  | AdmissionDateTime | datetime     | NO   |     | NULL    |       |
  | AdmissionEvent    | varchar(255) | YES  |     | NULL    |       |
  | DischargeDateTime | datetime     | YES  |     | NULL    |       |
  | DischargeEvent    | varchar(255) | YES  |     | NULL    |       |
  | DemographicId     | bigint(20)   | NO   | MUL | NULL    |       |
  | FacilityId        | bigint(20)   | YES  | MUL | NULL    |       |
  | VisitId           | bigint(20)   | NO   | MUL | NULL    |       |
  | WardId            | bigint(20)   | NO   | MUL | NULL    |       |
  +-------------------+--------------+------+-----+---------+-------+
  </code></pre>
  
  <p>On which I run the following JPA (Spring-data) query:</p>
  
  <pre><code>@Query("SELECT w FROM WardTransaction w WHERE w.id = (SELECT MAX(x.id) FROM
  WardTransaction x WHERE w = x AND w.visit = :visit)")
  public WardTransaction findCurrent(@Param("visit") Visit visit);
  </code></pre>
  
  <p>On occasions I get the following exception.</p>
  
  <pre><code>org.springframework.dao.IncorrectResultSizeDataAccessException: More than one
  result was returned from Query.getSingleResult(); nested exception is
  javax.persistence.NonUniqueResultException: More than one result was returned from
  Query.getSingleResult()
  </code></pre>
  
  <p>I have not been able to work out why this is happening.  It does not seem to make a lot of sense to me as there can only be one 'MAX' - especially on Id (I have used 'admissionDate' in the past).</p>
  
  <p>Any assistance appreciated.</p>