Announcement Announcement Module
Collapse
No announcement yet.
dynamic mongo repository Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dynamic mongo repository

    Hi,

    I am looking for a solution for two days... Maybe someone can help.. My problem:

    I have some classes which are documents (Annotated with @Document(collection="..."). Those extend an abstract Document (Let's call it AbstractDocument). They are all using the same collection.

    Second I have a MongoRepository<AbstractDocument, String>. When I now use the repository, it says that there cannot an object of AbstractDocument be created, of course. The _class-Property is set correctly to the extending document class but obviously it is ignored..

    Is there a way to use one repository for several document classes?


    Maybe it is better when I describe the problem:

    I have some kind of cms which uses widgets (which are the documents). All widgets extends one abstract widget. Each widget is an document. When I fetch all widgets of a page, I want all belonging widgets in one list. So I thought, I could use MongoRepository<AbstractDocument, String>.
    Last edited by xm22; Oct 23rd, 2012, 08:49 AM.

  • #2
    Just another idea: Is there a way to implement a discriminator map?

    Comment


    • #3
      I found out a strange thing. The problem so far was, that the spring-data-mapping-mechanism was obviously not able to connect the _class-Attribute to the Document-class.

      But: When I first persist a Document into the database, then I can receive the documents without any problems (And with AbstractDocument in my MongoRepository). Is there any caching of mappings which is started on writing first?

      Comment


      • #4
        I found a "solution". It seems to be, that the mapping is created first when an object has been converted into a DBObject. The solution is to convert one object of each class which can be stored in the collection one time. After this the converter seems to know about the mapping.

        Is this a bug? Do I do something wrong? I appended my configuration class:

        Code:
        package com.rebuy.service.cm.configuration;
        
        import java.net.UnknownHostException;
        
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.PropertySource;
        import org.springframework.data.authentication.UserCredentials;
        import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
        import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
        import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
        
        import com.mongodb.Mongo;
        import com.mongodb.WriteConcern;
        import com.rebuy.service.cm.persistence.document.widget.ImageWidget;
        import com.rebuy.service.cm.persistence.document.widget.TextWidget;
        
        @Configuration
        @EnableMongoRepositories
        @PropertySource("classpath:/${env}/nosql.properties")
        public class MongoDbConfiguration extends AbstractMongoConfiguration
        {
        	@Value("${mongo.host}")
        	private String host;
        	
        	@Value("${mongo.port}")
        	private Integer port;
        
        	@Value("${mongo.dbname}")
        	private String dbname;
        
        	@Value("${mongo.secure}")
        	private Boolean secure;
        
        	@Value("${mongo.username}")
        	private String username;
        
        	@Value("${mongo.password}")
        	private String password;
        
        	@Value("${mongo.typeKey}")
        	private String typeKey;
        	
        	@Override
        	@Bean
        	public Mongo mongo() throws UnknownHostException
        	{
        		Mongo mongo = new Mongo(host, port);
        		mongo.setWriteConcern(WriteConcern.SAFE);
        		return mongo;
        	}
        	
        	@Override
        	protected String getDatabaseName()
        	{
        		return dbname;
        	}
        	
        	@Override
        	protected UserCredentials getUserCredentials() {
        		if (secure) {
        			return new UserCredentials(username, password);
        		}
        		
        		return null;
        	}
        	
        	@Bean
        	@Override
        	public MappingMongoConverter mappingMongoConverter() throws Exception {
        		MappingMongoConverter converter = super.mappingMongoConverter();
        		converter.convertToMongoType(new TextWidget());
        		//converter.convertToMongoType(new ImageWidget());
        		registerCustomConverters(converter);
        		return converter;
        	}
        	
        	private void registerCustomConverters(MappingMongoConverter converter)
        	{
        		//List<Converter<Object, Object>> conversions= new ArrayList<Converter<Object, Object>>();
        		//converter.setCustomConversions(new CustomConversions(conversions));
        	}
        }
        Last edited by xm22; Oct 24th, 2012, 07:51 AM.

        Comment

        Working...
        X