Spring Boot OAuth2项目遇Hibernate NoClassDefFoundError求助
org.hibernate.metamodel.source.annotations.JPADotNames NoClassDefFoundError in Spring Boot OAuth2 Project Hey there, let's tackle this error you're running into. The NoClassDefFoundError for JPADotNames is almost always a Hibernate version compatibility issue—here's why and how to fix it:
What's Causing the Error?
Looking at your stack trace, you can see your app is trying to use hibernate-entitymanager-4.3.8.Final, but the JPADotNames class was introduced in Hibernate 5.x. Spring Boot 1.5.8 (which you're using) is designed to work with Hibernate 5.0.x by default, so there's a dependency conflict somewhere pulling in an older Hibernate version.
Step-by-Step Fixes
1. Identify the Conflicting Dependency
First, run this Maven command to generate your dependency tree and find which library is bringing in the old Hibernate:
mvn dependency:tree
Look for entries pointing to hibernate-entitymanager:4.3.8.Final—note the parent dependency that's pulling it in.
2. Exclude the Old Hibernate Dependency
Once you've found the culprit, add exclusions to that dependency in your pom.xml to force it to use Spring Boot's default Hibernate version. For example, if a third-party library is causing the conflict:
<dependency> <groupId>[conflicting-group-id]</groupId> <artifactId>[conflicting-artifact-id]</artifactId> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </exclusion> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </exclusion> </exclusions> </dependency>
3. Clean Up Your Pom.xml (Optional but Recommended)
Your current pom has some redundant dependencies that can cause version mismatches:
- Remove the explicit
junitdependency—spring-boot-starter-testalready includes it with the correct version. - Remove the explicit
spring-boot-testdependency—again,spring-boot-starter-testcovers this, and specifying the version manually can break alignment with your Spring Boot parent.
Here's a trimmed-down version of your dependencies section for reference:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Security Basic & OAuth2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> <!-- --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <!-- Java Persistence API --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- H2 database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
4. Verify and Rebuild
After making these changes, run mvn clean install to rebuild your project, then start your app again. The NoClassDefFoundError should be resolved once the correct Hibernate version is used.
Quick Note on Your User Entity
Your User entity implementing UserDetails looks fine—no issues there that would cause this error. The problem is strictly related to dependency versions.
内容的提问来源于stack exchange,提问作者Jotaro

