Eclipse Indigo - JPA Validation Problems

2019-05-10 15:38发布

问题:

I'm using eclipse indigo and am having "JPA Validation Problems".

My named query is:

from Person p where p.name = :name

and there is this error:

The query does not start with a valid identifier, has to be either SELECT, UPDATE or DELETE FROM.

But it's a valid JPQL query. Somebody know how I can remove this error?

If I change my query to

select p from Person p where p.name = :name

there is no more error, but I do not want to change all my queries.

thanks

mp5

回答1:

It looks to me like any queries that are of the form:

from Person p where p.name = :name

Are not in fact valid JPQL. According to the language reference at:

http://docs.oracle.com/javaee/5/tutorial/doc/bnbuf.html

each statement needs to have either a SELECT, UPDATE or DELETE statement preceding the FROM portion.

Here are more examples:

http://en.wikipedia.org/wiki/Java_Persistence_Query_Language

Unfortunately, it looks like you need to update all the queries to make them fit this format.



回答2:

If you are not concerned with portability, you can turn off the JPQL validation that was added to Dali in the the Indigo release. If you have a JPA project with the Hibernate platform selected you will still get whatever validtion Hibernate Tools has for JPQL/HQL.

Go to workspace preferences 'Java Persistence'->JPA->Errors/Warnings' under 'Queries and generators' and change 'Invalid or incomplete JPQL queries' to 'Ignore'. You can enter a bug against the Hibernate tools if you would like them to extend the Dali JPQL validation for the Hibernate platform or just to turn it off by default.



回答3:

It's not a valid JPQL query. It's a valid HQL query, but HQL ain't JPQL. A JPQL query must have a select clause.

Here's the BNF syntax of a JPQL clause, from the specifications:

select_statement :: = select_clause from_clause [where_clause] [groupby_clause [having_clause] [orderby_clause]


回答4:

And indeed that is not a valid JPQL query. JPQL starts with "SELECT", "UPDATE" or "DELETE".

Obviously it may work in Hibernate (i.e HQL) but that query is not standard and not portable. So if you don't want to change your queries then you aren't using JPA, and your app is non-portable.

The JPA spec would confirm this.