Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FROM clause of query has class XXX but no alias

Tags:

java

jpa

In my GAE project I started with JPA 1.0 and this code worked great:

    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);

But now that I working with JPA 2.0 I get:

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)

I manged to get it work with CriteriaQuery but the code is not readable and a little bit messy.

Any idea how to fix this exception?

like image 387
Rami Avatar asked Jun 22 '12 12:06

Rami


1 Answers

I think you need to define an alias for your class like this:

Query query = em.createQuery("SELECT i FROM MyImage i " + 
                             "WHERE i.m_Email = :email " + 
                             "And i.m_Password = :password ", MyImage.class);
like image 156
McIntosh Avatar answered Sep 23 '22 11:09

McIntosh