Good day,
I have this weird problem:
This following statement works
Query q = em.createQuery("SELECT m from AccountClass as m");
whereas this following statement does not
Query q = em.createQuery("SELECT m from AccountClass");
I'm trying to write a statement that allows me to use the where clause;
thank you for reading this.
em.createQuery
expects a JPQL query and not a SQL query to be passed as a parameter.SELECT m FROM AccountClass
is not a valid JPQL query, where asSELECT m FROM AccountClass m
is valid. If you wish to learn further about JPQL, you can start with the Java EE tutorial chapter on JPQL.The reason why
SELECT m FROM AccountClass as m
works in this case, is becauseAS
is an optional keyword. If you wish to issue a WHERE clause, it is trivial to do so -SELECT m FROM AccountClass m WHERE m.x= :param1
, wherex
is an attribute of theAccountClass
class andparam1
is a named parameter whose value has to be set using theQuery.setParameter
method.