I am trying to query a table called student
in MySQL. This is the structure:
Here is the java code which I use to query:
package com.luv2code.hibernate.demo; import java.util.*; import org.hibernate.*; import org.hibernate.cfg.*; import com.luv2code.hibernate.demo.entity.Student; public class QueryStudentDemo { public static void main(String[] args) throws Exception { SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Student.class) .buildSessionFactory(); Session session = factory.getCurrentSession(); try { session.beginTransaction(); List<Student> list = session.createQuery("from Student").getResultList(); session.getTransaction().commit(); System.out.println(list); } finally { factory.close(); } } }
This is the error message:
May 25, 2022 6:40:05 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate ORM core version 5.6.5.Final May 25, 2022 6:40:06 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final} May 25, 2022 6:40:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) May 25, 2022 6:40:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC] May 25, 2022 6:40:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {password=****, user=hbstudent} May 25, 2022 6:40:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false May 25, 2022 6:40:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 1 (min=1) May 25, 2022 6:40:07 AM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect May 25, 2022 6:40:07 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC] May 25, 2022 6:40:07 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections close ERROR: Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC May 25, 2022 6:40:07 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl validateConnectionsReturned ERROR: Connection leak detected: there are 1 unclosed connections! Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at jdk.proxy2/jdk.proxy2.$Proxy46.createQuery(Unknown Source) at com.luv2code.hibernate.demo.QueryStudentDemo.main(QueryStudentDemo.java:23) Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:351) ... 2 more Caused by: java.lang.NoClassDefFoundError: antlr/RecognitionException at org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:44) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:112) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:636) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:748) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114) ... 7 more Caused by: java.lang.ClassNotFoundException: antlr.RecognitionException at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 14 more
Here is the image of the referenced libraries
:
Insertion into table is working correctly. Only querying isn’t.
What is the error here?
Please comment if extra information is needed.
UPDATE:
It worked when I added antlr
to my class path
Advertisement
Answer
As per exception stack trace, it looks like antlr/RecognitionException
class file is missing. Add below dependency and try. This should solve your issue.
<dependency> <groupId>org.antlr</groupId> <artifactId>antlr-complete</artifactId> <version>3.5.3</version> </dependency>