开发者

Using JPA1.0: How to write queries

开发者 https://www.devze.com 2023-03-22 11:44 出处:网络
Good day, I have this weird problem: This following statement works Query q = em.createQuery(\"SELECT m from AccountClass as m\");

Good day,

I have this weird problem:

This following statement works

Query q = em.createQuery("SELECT m from AccountClass as m");

whereas this following st开发者_运维百科atement 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 m.x= :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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号