简体   繁体   中英

Using JPA1.0: How to write queries

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 as SELECT 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 because AS is an optional keyword. If you wish to issue a WHERE clause, it is trivial to do so - SELECT m FROM AccountClass m WHERE mx=:param1 , where x is an attribute of the AccountClass class and param1 is a named parameter whose value has to be set using the Query.setParameter method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM