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

Eclipse运行Spring MVC+Hibernate时init方法调用失败问题

Fixing BeanCreationException with NoClassDefFoundError: org/hibernate/cfg/Configuration in Spring MVC + Hibernate Project

Hey there, let's tackle this issue head-on! That error stack trace tells us exactly where the problem lies: your application can't find the org.hibernate.cfg.Configuration class at runtime, which is a core Hibernate class. This almost always boils down to missing or misconfigured Maven dependencies. Here's how to fix it:

Step 1: Verify Your Hibernate Core Dependency in pom.xml

First, make sure you've explicitly added the Hibernate Core dependency to your pom.xml—this is the most common culprit. If it's missing, add it with a version compatible with your Spring MVC version (e.g., Spring 5.x works best with Hibernate 5.x, Spring 6.x pairs with Hibernate 6.x):

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <!-- Replace with your desired compatible version -->
    <version>5.6.14.Final</version>
</dependency>
  • Double-check that the version number is correct (no typos!) and that it's not marked as provided unless you're deploying to a server that supplies Hibernate (unlikely for most local development).

Step 2: Refresh Maven Dependencies in Eclipse

Even if you have the dependency in pom.xml, Eclipse might not have downloaded it correctly. Let's force a refresh:

  • Right-click your project → Maven → Update Project
  • Check the box for Force Update of Snapshots/Releases
  • Click OK and wait for Maven to re-download all dependencies.

After this, check your project's Maven Dependencies folder—you should see hibernate-core-[version].jar listed. If it's still missing, your Maven mirror might be having issues; try switching to a public mirror like Maven Central in your settings.xml.

Step 3: Ensure Dependencies Are Deployed to Your Web App

For web projects, we need to make sure Maven dependencies are packaged into the WEB-INF/lib folder when deploying:

  • Right-click your project → Properties → Deployment Assembly
  • Look for Maven Dependencies in the list. If it's not there:
    • Click Add → Java Build Path Entries → Select Maven Dependencies → Click OK

Step 4: Double-Check Spring Configuration

While the root cause is a missing class, it's worth confirming your Spring config (e.g., applicationContext.xml) is set up correctly for Hibernate:

  • If you're using LocalSessionFactoryBean, ensure it's pointing to the correct Hibernate configuration file (hibernate.cfg.xml) or using annotated classes directly.
  • Make sure there are no typos in bean definitions that might be causing unexpected classpath issues.

Once you've gone through these steps, try running your application again—this should resolve the BeanCreationException caused by the missing Hibernate class.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:40:23