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

Spring MVC整合MySQL遇Bean创建异常求助(Spring Data JPA+Hibernate)

Fix for EntityManagerFactory Bean Creation Exception in Spring MVC + MySQL 8 + Java 19

Root Causes

  1. Outdated MySQL Driver & Dialect: Legacy MySQL driver (com.mysql.jdbc.Driver) and MySQL5 dialect are incompatible with MySQL 8.0.
  2. Javassist-Java 19 Compatibility: The old Javassist version used by your Hibernate setup doesn't support Java 19's module system and reflection rules, leading to a NullPointerException during proxy creation.
  3. Spring Version Mismatch: Spring 5 (from the tutorial) has limited support for Java 19, as it's optimized for Java 8-11 LTS versions.

Step-by-Step Fixes

1. Update MySQL Driver & Dialect

Replace legacy configurations with MySQL 8.x-compatible versions:

  • Use the new MySQL driver class com.mysql.cj.jdbc.Driver
  • Switch to org.hibernate.dialect.MySQL8Dialect for proper MySQL 8 support
  • Append &serverTimezone=UTC to the JDBC URL to resolve timezone errors

Modified database.properties:

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/public?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
jdbc.user=root
jdbc.pass=root
hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
hibernate.bytecode.provider=bytebuddy

2. Replace Javassist with ByteBuddy

Avoid Javassist's Java 19 compatibility issues by switching Hibernate's bytecode provider to ByteBuddy:

  • Add hibernate.bytecode.provider=bytebuddy to database.properties (as shown above)
  • Ensure your build includes compatible ByteBuddy dependencies (Maven example):
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.15.Final</version> <!-- Match your Spring-compatible Hibernate version -->
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-bytecode-proxy</artifactId>
        <version>5.6.15.Final</version>
    </dependency>
    

3. Fix Configuration Class Hardcoding

Remove the hardcoded dialect in PersistenceJPAConfig and pull values from the properties file:

final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
    hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
    hibernateProperties.setProperty("hibernate.bytecode.provider", env.getProperty("hibernate.bytecode.provider", "bytebuddy"));
    return hibernateProperties;
}

4. Optional: Adjust Java/Spring Versions

  • Downgrade to Java 11: If you want to keep using Spring 5, Java 11 is the latest fully supported LTS version.
  • Upgrade to Spring 6: For Java 19 compatibility, upgrade to Spring Framework 6+ (note: this requires switching to Jakarta EE dependencies instead of Java EE).

Content sourced from Stack Exchange, question author janedoe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:48:08