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

IntelliJ中Hibernate连接MySQL失败:MySQLNonTransientConnectionException求助

Hey there, let's work through this Hibernate issue you're hitting in IntelliJ Ultimate! I’ve gone over your problem details, and here’s a breakdown of what might be going wrong and how to fix it:

Your Core Issue

You’re building your first Hibernate app with IntelliJ Ultimate (instead of the Eclipse-based tutorial you’re referencing), using Maven and MySQL. You’ve confirmed your database credentials/port are correct, but you’re stuck with this critical error:

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Likely Causes & Fixes

1. Duplicate Hibernate Core Dependency Conflict

Looking at your classpath output, you’ve got two conflicting versions of Hibernate Core loaded:

  • hibernate-core-5.2.16.Final.jar (from your project’s lib folder)
  • hibernate-core-5.3.0.Final.jar (from your local Maven repo)

Version clashes like this break Hibernate’s internal service initialization.
Fix:

  • Open your pom.xml and make sure you only declare one Hibernate Core dependency (stick with the newer 5.3.0.Final version for better compatibility)
  • Delete all manually added Hibernate jars from your project’s lib directory—let Maven handle all dependency management to avoid duplicates

2. Hibernate Configuration File Mistakes

The JdbcEnvironment error almost always ties to database connection config details, even if you think your credentials are right. Double-check these:

  • Ensure your hibernate.cfg.xml (or persistence.xml if using JPA) has the correct MySQL JDBC URL. For MySQL 5.x, it should look like this:
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db_name?useSSL=false&amp;serverTimezone=UTC</property>
    
    Replace your_db_name with your actual database name, and include the useSSL=false (to avoid SSL handshake issues) and serverTimezone parameters (required for Java 8+).
  • Verify hibernate.connection.driver_class is set to com.mysql.jdbc.Driver (matches your mysql-connector-java 5.1.6 version)
  • Make sure your config file is placed in the src/main/resources directory—IntelliJ marks this as a resource root by default, so Hibernate can find it. If not, right-click the folder and select Mark Directory as > Resources Root.

3. MySQL Service Isn’t Running

Double-check that your local MySQL service is up and running. You can test the connection directly via MySQL Workbench or the command line to rule out this basic issue.

4. IntelliJ Project Configuration Checks

  • Confirm your project’s Project SDK is set to JDK 1.8 (matches the jdk1.8.0_162 you’re using to run the app)
  • Run Maven > Clean followed by Maven > Install to rebuild your project and clear any stale compiled files

Extra Tip

If none of these steps resolve the issue, share your hibernate.cfg.xml and pom.xml code snippets—this will help pinpoint the exact problem faster.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:16:56