我是JPA的新手,当我尝试使用MAX()函数查询数据库时遇到问题。 我的功能代码如下。谁能帮我?谢谢。
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;
}
我正在使用JPA,TopLink和Apache Derby。我的方法应该返回表用户的最大id。
编辑:我从服务中调用该函数:
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.");
}
user.setId()
的值始终为'0'。
编辑(2):记录
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.<init>(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:126)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1475)
... 35 more
我的实体User
声明如下:
@Entity
@Table(name = "user")
public class User {
@Id
private int id;
private String name;
private String lastName;
private String city;
private String password;
答案 0 :(得分:20)
您可以直接使用更简单的JPQL
return (Integer)entityManager.createQuery("select max(u.id) from User u").getSingleResult();
或使用TypedQuery
return entityManager.createQuery("select max(u.id) from User u", Integer.class).getSingleResult();
修改强>
Unknown entity type [user]
您应该使用User
代替user
。
答案 1 :(得分:3)
从您的评论中很难说,您还没有发布任何日志记录。
这个怎么样:
Query query = entityManager.createQuery("SELECT u FROM users u WHERE u.id = (SELECT MAX(u.id) FROM users u)");
答案 2 :(得分:1)
我正在使用该代码:
Query qry = em.createQuery("SELECT MAX(t.column) FROM Table t");
Object obj = qry.getSingleResult();
if(obj==null) return 0;
return (Integer)obj;
如果表“t”上没有元素,则抛出NullpointerException。
答案 3 :(得分:-2)
这是一个很好的解决方案,但你必须在sql的两个部分中使用不同的名称。像这样: “SELECT u FROM users u WHERE u.id =(SELECT MAX(u2.id)FROM users u2)”