title: Unable to have correct value with SELECT query using MAX() in JPA
body: <p>I am new in JPA and I have a problem when I try to query to the database using MAX() function.
  Code of my function is following. Can anyone help me? Thank you.</p>
  
  <pre><code>public int getMaxId(){
  
      entityManager = this.entityManagerFactory.createEntityManager();
  
      Query query = entityManager.createQuery("SELECT * FROM user WHERE id = (SELECT MAX(u.id) FROM user u)");
      User user = (User) query.getSingleResult();
  
      int id = user.getId();
      return id;
  }
  </code></pre>
  
  <p>I am using JPA, TopLink and Apache Derby. My method should return the maximum id of table users.</p>
  
  <p>Edit: I call that function from a service:</p>
  
  <pre><code>try {
          int id = userDAO.getMaxId();
          logger.info("Max id: " + id);
          user.setId(id+1);
  
      }
      catch (Exception ex){
          logger.error("Unable to get the max id.");
      }
  </code></pre>
  
  <p>Value of <code>user.setId()</code> is always '0'.</p>
  
  <p>Edit(2): Log</p>
  
  <pre><code>    Caused by: Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JPQLException
  Exception Description: Error compiling the query [SELECT u FROM user u WHERE u.id = (SELECT MAX(uu.id) FROM user uu)]. Unknown entity type [user].
      at org.eclipse.persistence.exceptions.JPQLException.entityTypeNotFound(JPQLException.java:483)
      at org.eclipse.persistence.internal.jpa.parsing.ParseTreeContext.classForSchemaName(ParseTreeContext.java:138)
      at org.eclipse.persistence.internal.jpa.parsing.SelectNode.getClassOfFirstVariable(SelectNode.java:327)
      at org.eclipse.persistence.internal.jpa.parsing.SelectNode.getReferenceClass(SelectNode.java:316)
      at org.eclipse.persistence.internal.jpa.parsing.ParseTree.getReferenceClass(ParseTree.java:436)
      at org.eclipse.persistence.internal.jpa.parsing.ParseTree.adjustReferenceClassForQuery(ParseTree.java:75)
      at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateReadQueryInternal(JPQLParseTree.java:103)
      at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateQuery(JPQLParseTree.java:84)
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:219)
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:190)
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.&lt;init&gt;(EJBQueryImpl.java:142)
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.&lt;init&gt;(EJBQueryImpl.java:126)
      at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1475)
      ... 35 more
  </code></pre>
  
  <p>My entity <code>User</code> is declared as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "user")
  public class User {
  
  @Id
  private int id;
  private String name;
  private String lastName;
  private String city;
  private String password;
  </code></pre>