Results 1 to 4 of 4

Thread: MongoDB: How to use a custom converter for an embedded property / field

  1. #1

    Default MongoDB: How to use a custom converter for an embedded property / field

    Hi,

    in a project using mongoDB I have a bean with a custom id type that I want to get mapped to a DBObject. I followed the documentation (http://static.springsource.org/sprin.../html/#d0e2654) but the custom Id does not get converted.

    This is my bean:

    Code:
    @Document(collection = "products")
    public class ProductGroup {
    
       @Id
       private PortalContentId id;
    
       public PortalContentId getId() {
          return id;
       }
    
       public void setId(final PortalContentId id) {
          this.id = id;
       }
    
    }
    The converter:

    Code:
    public class PortalContentIdWriteConverter implements Converter<PortalContentId, DBObject> {
    
       @Override
       public DBObject convert(final PortalContentId source) {
          return BasicDBObjectBuilder.start("cid", source.getContentId()).add("appId", source.getAppId()).get();
       }
    
    }
    I have registered this in applicationContext.xml:

    Code:
    	<mongo:mapping-converter id="mongoMappingConverter"
    		base-package="com.s24.dataexport"
    		db-factory-ref="mongoDbFactory">
    		<mongo:custom-converters>
    			<mongo:converter><bean class="....PortalContentIdWriteConverter" /></mongo:converter>
    		</mongo:custom-converters>
    	</mongo:mapping-converter>
    
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    		<constructor-arg ref="mongoMappingConverter" />
    	</bean>
    When I invoke

    Code:
    mongoTemplate.insert(Arrays.asList(someProductGroup), ProductGroup.class);
    the following exception is thrown:

    Code:
    java.lang.IllegalArgumentException: can't serialize class ....PortalContentId
    	at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:234)
    	at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
    	at org.bson.BSONEncoder.putObject(BSONEncoder.java:86)
    	at com.mongodb.OutMessage.putObject(OutMessage.java:190)
    	at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:253)
    	at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:217)
    	at com.mongodb.DBCollection.insert(DBCollection.java:102)
    	at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:690)
    	at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:686)
    	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:329)
    	at org.springframework.data.mongodb.core.MongoTemplate.insertDBObjectList(MongoTemplate.java:686)
    	at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:629)
    	at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:578)
    Is this simply not (yet) supported, or what is the problem here?

    Thanx && cheers,
    Martin

  2. #2
    Join Date
    Jan 2012
    Posts
    1

    Default Figure it out?

    I have a similar problem. Did you figure this out? All insight appreciated.
    Jason

  3. #3

    Default

    Nope, instead I switched to morphia again.

    Cheers,
    Martin

  4. #4
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    What version did you try this with? Would you mind trying with the latest 1.0 release?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •