Error with JPQL

2019-09-01 08:03发布

问题:

I have a problem with JPA and JPQL. I have the next query:

String query="SELECT c from Cliente c";
Query quer=em.createQuery("Select c from Cliente c");
List<Cliente> lista= quer.getResultList();

When I execute that, i hace the following error :

[java] Exception in thread "main" java.lang.NoClassDefFoundError: antlr/RecognitionException [java] at org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:59) [java] at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:103) [java] at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80) [java] at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) [java] at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) [java] at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) [java] at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735) [java] at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) [java] at test.TestCliente.main(TestCliente.java:35) [java] Caused by: java.lang.ClassNotFoundException: antlr.RecognitionException [java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366) [java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355) [java] at java.security.AccessController.doPrivileged(Native Method) [java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354) [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:423) [java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:356) [java] ... 9 more

The class CLiente is accesible, but I cant make a query.

Anyone knows why?

EDIT: In the classpath is the class RecognitionException and I can import him, so i dont know what is the problem.

回答1:

You most likely have the wrong antlr jar. I just looked it up in one of my maven projects that uses hibernate.

I have hibernate-core-4.1.8 and antlr-2.7.7, it works there.

You can always lookup the correct version in the pom.xml of hibernate-core of your specific version.

Be sure to fix the two places for the correct version number e.G. for 4.1.8:

http://repo1.maven.org/maven2/org/hibernate/hibernate-core/4.1.8.Final/hibernate-core-4.1.8.Final.pom
                                                           ^ ^ ^                      ^ ^ ^

or browse to the version overview: http://repo1.maven.org/maven2/org/hibernate/hibernate-core/ and select your version, then 'pom.xml'. Look in the dependencies section for the antlr entry and there you go.



标签: jpa jpql