Announcement Announcement Module
Collapse
No announcement yet.
MongoDB: How to use a custom converter for an embedded property / field Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Figure it out?

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

    Comment


    • #3
      Nope, instead I switched to morphia again.

      Cheers,
      Martin

      Comment


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

        Comment

        Working...
        X