Hibernate 早期版本依賴大量第三方 JAR,缺少任一依賴即拋 NoClassDefFoundError。本文整理 10 大高頻錯誤與對應 JAR 解決方案。
| # | 錯誤訊息 | 缺少 JAR | 用途 |
|---|---|---|---|
| 1 | NoClassDefFoundError: org/dom4j/DocumentException | dom4j.jar | 解析 hibernate.cfg.xml |
| 2 | NoClassDefFoundError: org/apache/commons/logging/LogFactory | commons-logging.jar | 日誌門面 |
| 3 | NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap | commons-collections.jar | 內部集合實現 |
| 4 | NoClassDefFoundError: net/sf/ehcache/CacheException | ehcache.jar | 二級快取 |
| 5 | 警告: No configuration found. Configuring ehcache from ehcache-failsafe.xml | ehcache.xml (重命名 failsafe) | 快取配置檔 |
| 6 | ClassNotFoundException: net.sf.cglib.transform.impl.InterceptFieldEnabled | cglib-full-2.0.02.jar | 字節碼增強(舊版) |
| 7 | NoSuchMethodError: net.sf.cglib.proxy.Enhancer.setInterceptDuringConstruction(Z)V | cglib-nodep-2.1_3.jar | 字節碼增強(新版) |
| 8 | NoClassDefFoundError: javax/transaction/Synchronization | jta.jar | JTA 事務 API |
| 9 | NoClassDefFoundError: antlr/ANTLRException | antlr-2.7.5h3.jar | HQL/AST 解析 |
| 10 | SQLException: The statement has no open cursor | 配置 <property name="jdbc.fetch_size">50</property> 註解掉 | MySQL 驅動相容性 |
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
mvn dependency:tree / gradle dependencies 找出衝突cglib-nodep,勿重複引入src/main/resources,而非 JAR 內