我正在尝试通过Spring的mongoTemplate连接mongoDb。但是,在对数据库执行CRUD操作时,我收到java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentEntity.hasVersionProperty()Z
错误
这是我的pom.xml
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</exclusion>
<!-- <exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion> -->
<!-- <exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion> -->
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
我的mongoconfiguration文件
@Configuration
public class SpringMongoConfig1 {
@Bean
MongoDbFactory mongoDbFactory() throws Exception{
return new SimpleMongoDbFactory(new MongoClient(), "storeApp");
}
@Bean
MongoTemplate mongoTemplate() throws Exception{
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
项目的主要类别
public class StoreMainApp {
public static void main(String[] args) {
System.out.println("into main method");
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig1.class);
MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate");
Store store1 = new Store("Sample store 1", "Street 1", "City 1", (float) 35.4);
System.out.println("store1 city : "+store1.storeCity);
mongoOperation.insert(store1);
}
}
如果我使用mongoOperation.createCollection(“stores”);,它工作正常,并且在数据库中创建了具有名称存储的集合,但是任何mongoOperation.save()或mongoOperation.insert()方法都没有运行。 / p>
关注堆栈跟踪:
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentEntity.hasVersionProperty()Z
at org.springframework.data.mongodb.core.MongoTemplate.initializeVersionProperty(MongoTemplate.java:669)
at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:651)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:613)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:604)
at com.storeApp.core.StoreMainApp.main(StoreMainApp.java:33)
答案 0 :(得分:1)
发出mvn dependency:tree -Dverbose
会显示您是否有任何jar冲突。我自己执行了该命令,在mongo-java-driver
引用2.11.0
org.springframework.data:spring-data-mongodb:jar:1.2.0.RELEASE
版本org.mongodb:mongo-java-driver:jar:2.10.1
[INFO] +- org.springframework:spring-core:jar:3.2.2.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.springframework:spring-context:jar:3.2.2.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:3.2.2.RELEASE:compile
[INFO] | | +- aopalliance:aopalliance:jar:1.0:compile
[INFO] | | +- (org.springframework:spring-beans:jar:3.2.2.RELEASE:compile - omitted for duplicate)
[INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate)
[INFO] | +- org.springframework:spring-beans:jar:3.2.2.RELEASE:compile
[INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate)
[INFO] | +- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate)
[INFO] | \- org.springframework:spring-expression:jar:3.2.2.RELEASE:compile
[INFO] | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate)
[INFO] +- org.mongodb:mongo-java-driver:jar:2.11.0:compile
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.2.0.RELEASE:compile
[INFO] | +- org.springframework:spring-tx:jar:3.1.4.RELEASE:compile
[INFO] | | +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] | | +- (org.springframework:spring-aop:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | | +- (org.springframework:spring-context:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | | \- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | +- (org.springframework:spring-context:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | +- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | +- (org.springframework:spring-expression:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | +- org.springframework.data:spring-data-commons:jar:1.5.0.RELEASE:compile
[INFO] | | +- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE)
[INFO] | | +- (org.slf4j:slf4j-api:jar:1.7.1:compile - omitted for conflict with 1.7.21)
[INFO] | | \- (org.slf4j:jcl-over-slf4j:jar:1.7.1:runtime - omitted for conflict with 1.7.1)
[INFO] | +- (org.mongodb:mongo-java-driver:jar:2.10.1:compile - omitted for conflict with 2.11.0)
[INFO] | +- (org.slf4j:slf4j-api:jar:1.7.1:compile - omitted for conflict with 1.7.21)
[INFO] | \- (org.slf4j:jcl-over-slf4j:jar:1.7.1:compile - scope updated from runtime; omitted for duplicate)
[INFO] \- cglib:cglib:jar:2.2.2:compile
[INFO] \- asm:asm:jar:3.3.1:compile
因此,请确保您有相同版本的冲突罐。
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<强>更新强>
这个 pom.xml 对我有用:
$sql = "SELECT * FROM slice_img ORDER BY id";
$result = mysqli_query($con, $sql);
while($data = mysqli_fetch_array($result))
{
echo "<div style='width:627px; margin:auto; padding-bottom: 5%;' class='container zeroBorder'>";
$text = $data['gen_number'];
$subfile = substr($text, ($i*2), 2);
echo '<img src="folder'.$data['id'].'/crop'.$subfile.'.jpg" class="zeroBorder" border="0" />';
echo "</div>";
}
确保清理并构建项目。