求助:导入SessionFactory时遇org.hibernate包多模块冲突问题
Hey there! Let's work through this package conflict issue you're hitting with Hibernate. That error pops up because the Java module system (introduced in Java 9) detects that the org.hibernate package is being exported by two different modules—hibernate.commons.annotations and hibernate.core—which breaks module rules. Here's how to fix it:
Remove redundant Hibernate dependencies
Starting with Hibernate 5.3+, thehibernate-commons-annotationslibrary is already bundled intohibernate-core. If you added both dependencies separately (like in your pom.xml or build.gradle), delete the standalonehibernate-commons-annotationsentry entirely. This is the most common root cause of this conflict.Exclude conflicting transitive dependencies (if needed)
Sometimes other dependencies might pull inhibernate-commons-annotationsas a transitive dependency. For Maven, explicitly exclude it from yourhibernate-coredependency:<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>your-hibernate-version</version> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> </exclusion> </exclusions> </dependency>For Gradle, use this syntax:
implementation('org.hibernate:hibernate-core:your-hibernate-version') { exclude group: 'org.hibernate', module: 'hibernate-commons-annotations' }Clean up your module-info.java (if using Java modules)
If your project uses the Java Module System (you have amodule-info.javafile), make sure you only require theorg.hibernate.coremodule. Remove any line likerequires org.hibernate.commons.annotations;—those packages are already covered byrequires org.hibernate.core;.Clear IDE caches
IDEs can sometimes hold onto outdated module metadata. Try invalidating caches and restarting:- In IntelliJ IDEA: Go to
File > Invalidate Caches...and select "Invalidate and Restart" - In Eclipse: Go to
Project > Clean...and rebuild your project
- In IntelliJ IDEA: Go to
Also, double-check that all your Hibernate dependencies use the same version—mixing different versions can trigger weird module conflicts too.
内容的提问来源于stack exchange,提问作者gaoquan liang

