我正在尝试将hibernate-shards与hibernate 4一起使用,但是我遇到了一个未找到类的错误。当我运行试图写入分片数据库的示例应用程序时,我得到以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/engine/SessionFactoryImplementor
at org.hibernate.shards.ShardedConfiguration.buildSessionFactory(ShardedConfiguration.java:251)
以下是我的Maven POM文件中的相关部分:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-shards</artifactId>
<version>3.0.0.Beta2</version>
</dependency>
查看hibernate-core jar文件,看来这个类的包与hibernate-shards所期望的不同。它似乎现在位于spi
目录中。
$ jar tvf lib/hibernate-core-4.0.0.Final.jar | grep SessionFactoryImplementor
3049 Wed Dec 14 22:47:16 EST 2011 org/hibernate/engine/spi/SessionFactoryImplementor.class
我找到了another question where people using hibernate-shards and Spring Framework have a similar issue。我没有使用Spring,所以我不认为这个问题的解决方案对我有用。
This question's回答表明可以使用hibernate-shards的分支来处理最新版本的hibernate,但是在该答案中指向fork的链接现在是一个错误。
我希望从源代码编译最新版本的hibernate-shards(从github克隆)可以解决问题。但是当我使用hibernate-shards-4.0.0-SNAPSHOT时,我得到了同样的异常,我这样创建:
# Get the hibernate-shards code
git clone https://github.com/hibernate/hibernate-shards.git
cd hibernate-shards
# Install the gradle build system. The following commands are for Ubuntu
sudo add-apt-repository ppa:cwchien/gradle
sudo apt-get update
sudo apt-get install gradle
# Compile the hibernate-shards project
gradle :jar :javadoc :sourcesJar
jar cf build/libs/hibernate-shards-4.0.0-SNAPSHOT-javadoc.jar -C build/docs/javadoc/ .
# Install hibernate-shards into maven
mvn install:install-file -DgroupId=org.hibernate -DartifactId=hibernate-shards -Dversion=4.0.0-SNAPSHOT -Dpackaging=jar -Dfile=build/libs/hibernate-shards-4.0.0-SNAPSHOT.jar -Djavadoc=build/libs/hibernate-shards-4.0.0-SNAPSHOT-javadoc.jar -Dsources=build/libs/hibernate-shards-4.0.0-SNAPSHOT-sources.jar
如果我查看gradle文件,它正在构建针对Hibernate 3.6.10.Final,而不是Hibernate 4。
此时hibernate分片是否与hibernate 4不兼容?