Closed spring-projects-issues closed 9 years ago
Florian Geßner commented
Here some implementation details:
@Configuration
@ComponentScan
@PropertySource("classpath:test-mongo-config.properties")
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class SpringIntegrationTestConfig {
//Implements AuditorAware<ObjectId> for spring data auditing but also a custom interface used for similar usages
@Bean
public DummyEditorProvider editorProvider() {
return new DummyEditorProvider();
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
public abstract class BaseMongoHistoryConfig {
public abstract EditorProvider editorProvider();
@Value("${spring.data.mongodb.ext.history.postfix:_hist}")
private String historyCollectionPostfix;
@Value("${spring.data.mongodb.ext.history.sortType:NATURAL}")
private String historySortType;
@Value("${spring.data.mongodb.ext.history.sortDirection:DESC}")
private String historySortDirection;
@Bean
public ConfigurationParameterHolder configurationParameterHolder() {
return new ConfigurationParameterHolder(historyCollectionPostfix, historySortType, historySortDirection);
}
@Bean
public MongoEntityHistoryRepository mongoVersionRepository() {
return new MongoEntityHistoryRepositoryBean(editorProvider(), mongoTemplate(), mongoEntityUtil(), configurationParameterHolder());
}
@Bean
public MongoEntityUtil mongoEntityUtil() {
return new MongoEntityUtil(mongoTemplate());
}
@Bean
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(mongo(), dbname());
}
@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}
public abstract String dbname();
public abstract MongoClient mongo();
@Bean
public HistoryAwareRepositoryAspect onSaveAspect() {
return new HistoryAwareRepositoryAspect(mongoVersionRepository());
}
}
@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "org.gessnerfl.persistence.springdata.mongodb.dummy.repo")
public class TestMongoConfig extends BaseMongoHistoryConfig {
private static final boolean IN_MEMORY_DB = true;
public static final String DATABASE = "dummy";
@Autowired
private EditorProvider editorProvider;
@Override
public EditorProvider editorProvider() {
return editorProvider;
}
@Override
public String dbname() {
return DATABASE;
}
@Bean
@Override
public MongoClient mongo() {
if (IN_MEMORY_DB) {
return fongo().getMongo();
} else {
return getLocalMongoDb();
}
}
private MongoClient getLocalMongoDb() {
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
return mongoClient;
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot connect to mongodb");
}
}
@Bean
public Fongo fongo() {
return new Fongo("InMemoryMongo");
}
}
@Document
public class DummyObject implements Serializable {
@Id
private ObjectId id;
@CreatedBy
private ObjectId createdBy;
@CreatedDate
private Date creaetdOn;
@LastModifiedBy
private ObjectId modifiedBy;
@LastModifiedDate
private Date modifiedOn;
private String name;
private DummyItem dummyItem;
private List<DummyItem> dummyItems = new ArrayList<>();
...
//Getter and setter
...
}
public class DummyItem {
private String name;
...
//Getter and setter
...
}
José Carlos Valero Sánchez commented
It is also happening to me on 1.7.0. It seems to appear when Audit is enabled. The bug disappears when I disable auditing capabilities.
Additionally, I am also using aspectJ. I can see from your traces aspectJ is also been used
Oliver Drotbohm commented
Have you tried extending AbstractMongoConfiguration
for the MongoDB specific parts of your configuration? It sets up a Spring bean for a MongoMappingContext
and pre-populates it with all classes annotated with @Document
underneath the package of that configuration class. The package to scan can be customized by overriding getMappingBasePackage()
.
This will make sure the entity metadata is available after bootstrap
Florian Geßner commented
using AbstractMongoConfiguration helped me to fix the issue. Thanks for your help
José Carlos Valero Sánchez commented
Ohh Oliver, it works now!
Thank you very much, not only for the "recipe" but particularly for taking the effort of sharing the in-depth reason of it.
Very much appreciated
Emircan Arıkök commented
If you are using java conf, below way could work but how to do in XML ? . Anyone know how to do it ? My xml configuration is like that.
<mongo:db-factory uri="**" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<mongo:repositories base-package="com.marl.data.repository" >
</mongo:repositories>
<mongo:auditing></mongo:auditing>
Paolo commented
there is no way to fix issue if i set a different id on mapping-converter element...but all work with default id="mappingConverter"
Paolo commented
Sorry for reopen...the problem is that mongo:auditing point to default mapping-context...so simply add this...
\
...and all work.
Can close, and sorry again, but documentation is not so clear
Florian Geßner opened DATAMONGO-1212 and commented
With the update to version 1.7.0 I get the following exception where persisting data via Repositories/MongoTemplate. With version 1.6.2 it is working fine. In the documentation there is nothing mentioned regarding changes related to this issue.
Affects: 1.7 GA (Fowler)
2 votes, 7 watchers