Hibernate应用启动报错:The application must supply JDBC connections
解决Hibernate报错:The application must supply JDBC connections
从报错日志和你提供的JDBC测试结果来看,问题核心是Hibernate未正确配置数据库连接参数。原生JDBC能正常连接,但Hibernate无法自动获取连接信息,导致它切换到"用户提供连接"模式,而你的代码并未手动提供连接,最终引发错误。同时,Hibernate无法通过JDBC元数据确定数据库方言,进一步导致服务初始化失败。
解决方案
方案1:通过配置文件(推荐)
方式A:使用hibernate.cfg.xml
在项目src/main/resources目录下创建或修改hibernate.cfg.xml,添加以下配置(替换为你的数据库信息):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "https://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- JDBC连接参数 --> <property name="jakarta.persistence.jdbc.url">jdbc:mysql://localhost:3306/myhiber</property> <property name="jakarta.persistence.jdbc.user">root</property> <property name="jakarta.persistence.jdbc.password">Mysql@1491714</property> <property name="jakarta.persistence.jdbc.driver">com.mysql.cj.jdbc.Driver</property> <!-- Hibernate方言(适配MySQL 8.0) --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> <!-- 可选:显示格式化后的SQL语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> </session-factory> </hibernate-configuration>
方式B:使用persistence.xml(Jakarta Persistence规范)
如果项目遵循Jakarta Persistence规范,在src/main/resources/META-INF目录下创建或修改persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd" version="3.0"> <persistence-unit name="MyHibernatePU" transaction-type="RESOURCE_LOCAL"> <class>org.example.YourEntityClass</class> <!-- 替换为你的实体类 --> <properties> <!-- JDBC连接参数 --> <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myhiber"/> <property name="jakarta.persistence.jdbc.user" value="root"/> <property name="jakarta.persistence.jdbc.password" value="Mysql@1491714"/> <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <!-- Hibernate方言 --> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/> </properties> </persistence-unit> </persistence>
方案2:编程式配置(代码中直接设置参数)
如果代码中直接使用Configuration类初始化SessionFactory,需手动添加JDBC配置:
package org.example; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class App { public static void main(String[] args) { Configuration configuration = new Configuration(); // 配置JDBC连接参数 configuration.setProperty("jakarta.persistence.jdbc.url", "jdbc:mysql://localhost:3306/myhiber"); configuration.setProperty("jakarta.persistence.jdbc.user", "root"); configuration.setProperty("jakarta.persistence.jdbc.password", "Mysql@1491714"); configuration.setProperty("jakarta.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver"); // 设置数据库方言 configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect"); // 添加项目中的实体类(若未在配置文件中声明) configuration.addAnnotatedClass(org.example.YourEntityClass.class); SessionFactory sessionFactory = configuration.buildSessionFactory(); // 后续业务操作... sessionFactory.close(); } }
验证步骤
- 确保配置文件放置在正确目录(
resources或META-INF下) - 替换配置中的实体类为项目实际的实体类
- 重新运行应用,检查是否成功初始化SessionFactory并连接数据库
内容的提问来源于stack exchange,提问作者Akash Deep Vishwakarma
相关产品推荐
相关产品推荐

