简体   繁体   中英

Syntax error in JPA query

When I execute the following code

return entityManager
            .createQuery("select a from Article where a.slug = ?1", Article.class)
            .setParameter(1, slug)
            .getSingleResult();

I get the following exception

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select a from Article where a.slug = '?1'], line 1, column 22: syntax error at [where].
Internal Exception: MismatchedTokenException(77!=78)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1328)

I'm using JPA 2 with EclipseLink 2.0.2.

What is wrong with my query?

......来自文章a ...(遗漏别名)

Well, the answer has alredy given.. But what I dont like about JPQL, you have to put an identifier after Entity name, but it is uncessary if your from clause does have only one Entity. Most of time, I also forget to put that unnecessarily required identifier. I wish I would write the above query as below;

select * from Article where slug = ?1

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