Niclas
Niclas

Reputation: 255

@EnableMongoAuditing for MongoDB on Cloud Foundry / mongolab

My setup works on my local but not when I deploy it to CloudFoundry/mongolab.

The config is very similar to the docs.

My local spring config

@Configuration
@Profile("dev")
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "com.foo.model")
public class SpringMongoConfiguration extends AbstractMongoConfiguration {

@Override
protected String getDatabaseName() {
    return "myDb";
}

@Override
public Mongo mongo() throws Exception {
    return new MongoClient("localhost");
}

@Bean
public AuditorAware<User> myAuditorProvider() {
    return new SpringSecurityAuditorAware();
}

}

This is the cloud foundry setup

@Configuration
@Profile("cloud")
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "com.foo.model")
public class SpringCloudMongoDBConfiguration extends AbstractMongoConfiguration {

private Cloud getCloud() {
    CloudFactory cloudFactory = new CloudFactory();
    return cloudFactory.getCloud();
}

@Bean
public MongoDbFactory mongoDbFactory() {
    Cloud cloud = getCloud();
    MongoServiceInfo serviceInfo = (MongoServiceInfo) cloud.getServiceInfo(cloud.getCloudProperties().getProperty("cloud.services.mongo.id"));
    String serviceID = serviceInfo.getId();
    return cloud.getServiceConnector(serviceID, MongoDbFactory.class, null);
}

@Override
protected String getDatabaseName() {
    Cloud cloud = getCloud();
    return cloud.getCloudProperties().getProperty("cloud.services.mongo.id");
}

@Override
public Mongo mongo() throws Exception {
    Cloud cloud = getCloud();
    return new MongoClient(cloud.getCloudProperties().getProperty("cloud.services.mongo.connection.host"));
}

@Bean
public MongoTemplate mongoTemplate() {
    return new MongoTemplate(mongoDbFactory());
}

@Bean
public AuditorAware<User> myAuditorProvider() {
    return new SpringSecurityAuditorAware();
}

}

And the error I'm getting when I try to save a document in Cloud Foundry is:

OUT ERROR: org.springframework.data.support.IsNewStrategyFactorySupport - Unexpected error
OUT java.lang.IllegalArgumentException: Unsupported entity com.foo.model.project.Project! Could not determine IsNewStrategy.
OUT at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:739)
OUT at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
OUT at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

Any ideas? Is it my config file etc..? Thanks in advance Niclas

Upvotes: 1

Views: 1406

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83171

This is usually caused if the Mongo mapping metadata obtained for entities does not scan entities at application startup. By default, AbstractMongoConfiguration uses the package of the actual configuration class to look for @Document annotated classes at startup.

The exception message makes me assume that SpringCloudMongoDBConfiguration is not located in any of the super packages of com.foo.model.project. There are two solutions to this:

  • Stick to the convenience of putting application configuration classes into the root package of your application. This will cause your application packages be scanned for domain classes, metadata obtained, and the is-new-detection work as expected.
  • Manually hand the package containing domain classes to the infrastructure by overriding MongoConfiguration.getMappingBasePackage().

The reason you might see the configuration working in the local environment is that the mapping metadata might be obtained through a non-persisting persistence operation (e.g. a query) and everything else proceeding from there.

Upvotes: 4

Related Questions