You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate与MySQL连接方法及报错求助:已尝试多种方法仍失败

Fixing Hibernate 5.2.16.Final + MySQL Connection Issues

Hey there! Let's work through your Hibernate-MySQL connection problem. From the logs you shared, I notice two key clues: the hibernate.properties not found info and the HHH90000012 warning about obsolete Hibernate configuration. Let's break down the fixes step by step.

1. Verify Dependencies First

Make sure you have the correct MySQL JDBC driver in your project, matching both your Hibernate version and MySQL server version:

  • If you're using MySQL 5.x, use mysql-connector-java:5.1.49 (fully compatible with Hibernate 5.2)
  • If you're using MySQL 8.0+, use mysql-connector-java:8.0.33 (note: this requires Java 8 or higher)

For Maven, add these dependencies to your pom.xml:

<!-- Hibernate Core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.16.Final</version>
</dependency>

<!-- MySQL JDBC Driver -->
<!-- For MySQL 5.x -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>
<!-- OR for MySQL 8.0+ -->
<!--
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>
-->

2. Fix Hibernate Configuration File

The hibernate.properties not found message just means Hibernate is falling back to your hibernate.cfg.xml (which is totally normal), but the HHH90000012 warning points to outdated configuration syntax. Use the modern Hibernate 5.2 DTD and correct connection properties:

Here's a complete, working hibernate.cfg.xml example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 5.2//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-5.2.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database Connection Settings -->
        <!-- For MySQL 5.x -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database_name?useSSL=false</property>
        <!-- For MySQL 8.0+ -->
        <!--
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=Asia/Shanghai</property>
        -->
        <property name="hibernate.connection.username">your_mysql_username</property>
        <property name="hibernate.connection.password">your_mysql_password</property>

        <!-- Hibernate Dialect (critical for proper SQL generation) -->
        <!-- For MySQL 5.x -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- For MySQL 8.0+ -->
        <!--
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        -->

        <!-- Optional Debug/Setup Settings -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Map Your Entity Classes -->
        <mapping class="com.yourpackage.YourEntityClass"/>
    </session-factory>
</hibernate-configuration>

3. Key Checks to Resolve Remaining Errors

  • Replace placeholders: Swap your_database_name, your_mysql_username, your_mysql_password with your actual database credentials.
  • Mandatory timezone for MySQL 8+: The serverTimezone parameter is required for MySQL 8.0+ to avoid timezone-related connection failures.
  • Dialect match: Always use the dialect corresponding to your MySQL version—using the wrong one can cause unexpected SQL syntax errors.
  • Test basic connection: Try connecting to your MySQL database with a simple JDBC test first to rule out network, credential, or database availability issues.

If you still hit errors, share the full, untruncated stack trace (your original log cuts off the obsolete config detail), but these steps should fix the most common issues with Hibernate 5.2 and MySQL.

内容的提问来源于stack exchange,提问作者park shin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:11:20