Reputation: 155
I upgrade my spring-data-mongodb (using maven) version to 1.9.5.RELEASE. (using MongoClient instead of Mongo). My Session document class contains @LastModifiedDate annotation.
After the upgrade, i got java.lang.IllegalArgumentException: Unsupported entity com...Session! Could not determine IsNewStrategy.
If i remove from the configuration i don't get the error but it doesn't save the modifiedDate. All the solutions I found related to MongoDB configuration using annotations and not xml file. How can i solve this?
Maven dependencies:
<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 configuration contain :
<?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>
My Document:
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
}
Upvotes: 2
Views: 1331
Reputation: 19000
The trick is to share a MongoMappingContext
between auditing and the MongoTemplate
.
First replace:
<mongo:auditing/>
with:
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<mongo:auditing mapping-context-ref="mappingContext"/>
Then add a MongoDbFactory
and a MongoConverter
to your xml settings as follows:
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="${dbName}"/>
<mongo:mapping-converter id="mongoConverter" db-factory-ref="mongoDbFactory" mapping-context-ref="mappingContext"/>
Last thing to do, is to inject the MongoConverter
into the template. It supplies the MongoTemplate
with the mapping context. You will have to use an alternate constructor for that.
Replace:
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="${dbName}"/>
with:
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mongoConverter"/>
Upvotes: 3