查询的FROM子句有类XXX但没有别名

时间:2012-06-22 12:31:52

标签: java jpa

在我的GAE项目中,我从JPA 1.0开始,这段代码很有用:

    Query query = em.createQuery("SELECT FROM MyImage " +
                                 "WHERE m_Email = :email " +
                                 "And m_Password = :password ", MyImage.class);
    query.setParameter("email", email);
    query.setParameter("password", password);

但是现在我使用JPA 2.0了,我得到了:

FROM clause of query has class util.MyImage but no alias
org.datanucleus.store.query.QueryCompilerSyntaxException: FROM clause of query has class util.MyImage but no alias
at org.datanucleus.query.compiler.JavaQueryCompiler.compileFrom(JavaQueryCompiler.java:233)
at org.datanucleus.query.compiler.JPQLCompiler.compile(JPQLCompiler.java:79)
at org.datanucleus.store.query.AbstractJPQLQuery.compileInternal(AbstractJPQLQuery.java:269)
at org.datanucleus.store.query.Query.setImplicitParameter(Query.java:825)
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:458)
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:57)
at dataBase.DataBase.getMyImageFromDB(DataBase.java:173)

我想让它与CriteriaQuery一起工作,但代码不可读且有点乱。

知道如何修复此异常吗?

1 个答案:

答案 0 :(得分:12)

我认为你需要为你的类定义一个别名:

Query query = em.createQuery("SELECT i FROM MyImage i " + 
                             "WHERE i.m_Email = :email " + 
                             "And i.m_Password = :password ", MyImage.class);