当前位置: 首页 > 知识库问答 >
问题:

Spring数据MongoDB导致IllegalArgumentException:不受支持的实体无法确定IsNewStrategy

慎旭尧
2023-03-14

我将spring data mongodb(使用maven)版本升级到1.9.5。释放。(使用MongoClient而不是Mongo)。我的会话文档类包含@LastModifiedDate注释。

升级后,我得到了java。lang.IllegalArgumentException:不支持的实体com。。。一场无法确定IsNewStrategy。

如果我从配置中删除,我不会得到错误,但它不会保存modifiedDate。我找到的所有解决方案都与使用注释而不是xml文件的MongoDB配置相关。我如何解决这个问题?

Maven依赖项:

<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-mongodb</artifactId>
	<version>1.9.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.5</version>
</dependency>

Spring MongoDB配置包含:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/context classpath:org/springframework/context/factory/xml/spring-context-3.0.xsd
						http://www.springframework.org/schema/data/mongo classpath:org/springframework/data/mongo/factory/xml/spring-mongo-1.8.xsd
						http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.1.xsd
						http://www.springframework.org/schema/util classpath:org/springframework/util/factory/xml/spring-util-3.0.xsd">

	<bean class="com.....core.CascadingMongoEventListener"/>
	<bean class="com.....core.DataEventListener"/>
			
	<mongo:auditing/>
	
	<mongo:mongo-client id="mongo" host="${host}" port="${port}">
		<mongo:client-options connections-per-host="100"
	                   		  connect-timeout="30000"
	                   		  max-wait-time="15000" 	                   	                   		  
	                   		  socket-keep-alive="true"
	                   		  socket-timeout="60000" />		
	</mongo:mongo-client>
		
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongo" ref="mongo" />
		<constructor-arg name="databaseName" value="${dbName}"/>		
		<property name="writeConcern">
            <util:constant static-field="com.mongodb.WriteConcern.SAFE" ></util:constant>
        </property>        
	</bean>
		
	<mongo:repositories base-package="com.....daos" mongo-template-ref="mongoTemplate"> 		
	</mongo:repositories>
	
	<context:component-scan base-package="com.....services"></context:component-scan>
	
	<!-- translate any MongoExceptions thrown in @Repository annotated classes -->
	<context:annotation-config />	
</beans>

我的文档:

package com.....session;

import java.util.Date;
import java.util.UUID;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;

@Document(collection = "session")
public class Session {

	@Id
	protected UUID id;
	private DateTime creationDate;
	@LastModifiedDate
	private DateTime modifiedDate;

	public Session() {}
	
	//setters & getters	
}

共有1个答案

卢志行
2023-03-14

诀窍是在审核和MongoTemplate之间共享MongoMappingContext。

第一次替换:

<mongo:auditing/>

与:

<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<mongo:auditing mapping-context-ref="mappingContext"/>

然后将MongoDbFactoryMongoConverter添加到您的xml设置中,如下所示:

<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="${dbName}"/>
<mongo:mapping-converter id="mongoConverter" db-factory-ref="mongoDbFactory" mapping-context-ref="mappingContext"/> 

最后要做的是将MongoConverter注入模板。它为MongoTemboard提供映射上下文。您必须为此使用替代构造函数。

替换:

<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="${dbName}"/>    

与:

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mongoConverter"/>
 类似资料: