使用休眠查询表时出错
Error while querying tables using hibernate
我正在尝试查询 MySQL 中名为 student
的 table。这是结构:
这是我用来查询的 java 代码:
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();
}
}
}
这是错误信息:
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
这是 referenced libraries
的图像:
插入 table 工作正常。只有查询不是。
这里有什么错误?
如果需要更多信息,请发表评论。
更新:
当我将 antlr
添加到我的 class 路径时它起作用了
根据异常堆栈跟踪,似乎 antlr/RecognitionException
class 文件丢失。添加以下依赖项并尝试。这应该可以解决您的问题。
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr-complete</artifactId>
<version>3.5.3</version>
</dependency>
我正在尝试查询 MySQL 中名为 student
的 table。这是结构:
这是我用来查询的 java 代码:
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();
}
}
}
这是错误信息:
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
这是 referenced libraries
的图像:
插入 table 工作正常。只有查询不是。
这里有什么错误?
如果需要更多信息,请发表评论。
更新:
当我将 antlr
添加到我的 class 路径时它起作用了
根据异常堆栈跟踪,似乎 antlr/RecognitionException
class 文件丢失。添加以下依赖项并尝试。这应该可以解决您的问题。
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr-complete</artifactId>
<version>3.5.3</version>
</dependency>