1.mongoTemplate.findById(id, clazz, collectionName) query based on the primary key _id reports a nested exception is java.lang.NoClassDefFoundError: com/mongodb/MongoException$DuplicateKey
2. The problem is very strange. Some _ids can be found, no problem. Some _id checks will report the above error.
3. The mongodb used in the database uses spring to integrate mongodb and uses mongoTemplate to query.
4. The relevant configuration information is as follows:
<!-- MONGODB begin -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<!-- <version>1.10.1.RELEASE</version> -->
<version>1.4.2.RELEASE</version><!-- <version>1.4.2.RELEASE</version><version>1.7.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<!--<version>1.13.1.RELEASE</version> -->
<version>1.7.2.RELEASE</version><!--<version>1.7.2.RELEASE</version> <version>1.10.0.RELEASE</version> -->
</dependency>
<!-- 是否需要待考察 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version> <!-- <version>3.4.2</version> <version>2.11.3</version>-->
</dependency>
<!-- MONGODB end -->
5.<!--Set the database name used-->
<mongo:db-factory id="mongoDbFactory"
host="${mongo.host}"
port="${mongo.port}"
dbname="${mongo.dbname}"
username="${mongo.username}"
password="${mongo.password}"
/>
<!-- mongodb的模板 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<mongo:mapping-converter base-package="com.xxx.entity" />
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<!-- 配置mongodb映射类型 -->
<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<!-- 默认Mongodb类型映射 -->
<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey">
<null/><!-- 设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 -->
</constructor-arg>
</bean>
6.@Override
public T findById(Serializable id, String collectionName) {
return mongoTemplate.findById(id, clazz, collectionName);
}
7. This error nested exception is java.lang.NoClassDefFoundError: com/mongodb/MongoException$DuplicateKey. There is no definite answer on the Internet. I hope someone knows the reason? My reason for this is strange. Some queries based on id are no problem. Some IDs report this error as soon as they are checked. depressed. . .
PHP中文网2017-05-24 11:37:45
I tested it and found out that there is a problem with the mongo version. Just change to a lower version. . .