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’slibfolder)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.xmland 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
libdirectory—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(orpersistence.xmlif using JPA) has the correct MySQL JDBC URL. For MySQL 5.x, it should look like this:
Replace<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC</property>your_db_namewith your actual database name, and include theuseSSL=false(to avoid SSL handshake issues) andserverTimezoneparameters (required for Java 8+). - Verify
hibernate.connection.driver_classis set tocom.mysql.jdbc.Driver(matches your mysql-connector-java 5.1.6 version) - Make sure your config file is placed in the
src/main/resourcesdirectory—IntelliJ marks this as a resource root by default, so Hibernate can find it. If not, right-click the folder and selectMark 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 > Cleanfollowed byMaven > Installto 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

