Announcement Announcement Module
Collapse
No announcement yet.
Mongo Custom Converter not triggering. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mongo Custom Converter not triggering.

    I am working on spring data mongodb project and i have a complex object that not able to map using metadata mapping because it has composite key. I wrote a custom converter and trying to use mongoTemplate.find() but its not triggering the custom converter mapping.

    Configuration:

    Code:
    <!-- MongoDB Factory -->
    	<mongo:mongo id="mongo" host="${mongo.host.name}" port="${mongo.host.port}">
    		<mongo:options connections-per-host="100"
    			threads-allowed-to-block-for-connection-multiplier="100"
    			connect-timeout="30000" max-wait-time="10000" auto-connect-retry="true"
    			socket-keep-alive="false" socket-timeout="100000" slave-ok="false"
    			write-number="1" write-timeout="0" write-fsync="true" />
    	</mongo:mongo>
    	<mongo:db-factory dbname="${mongo.db.name}" password="${mongo.password}"
    		username="${mongo.user.name}" mongo-ref="mongo" />
    	<!-- Mongo custom converters -->
    	<mongo:mapping-converter base-package="com.ranker.app.mongo.domain">
    		<mongo:custom-converters
    			base-package="com.ranker.app.mongo.customconverter.agglist.converter" />
    	</mongo:mapping-converter> 
    	<!-- Offers convenience methods and automatic mapping between MongoDB JSON 
    		documents and your domain classes. -->
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    		<constructor-arg name="mongoConverter" ref="mappingConverter" />
    	</bean>

    converters:

    Code:
    package com.ranker.app.mongo.customconverter.agglist.converter;
    
    import java.util.Date;
    
    import org.springframework.core.convert.converter.Converter;
    
    import com.mongodb.DBObject;
    import com.ranker.app.mongo.domain.list.ListAggView;
    import com.ranker.app.mongo.domain.list.ListViewId;
    import com.ranker.app.mongo.domain.list.View;
    
    public class ListAggViewReadConverter implements Converter<DBObject, ListAggView>
    {
    
        public ListAggView convert(DBObject source)
        {
            ListAggView listAggView = new ListAggView();
            View view = new View();
            ListViewId listViewId = new ListViewId();
            listViewId.setEventDate((Date) source.get("_id.d"));
            listViewId.setLid((Integer) source.get("_id.lid"));
            view.setCount((Integer) source.get("value.count"));
            view.setMean((Integer) source.get("value.mean"));
            view.setEventDate((Date) source.get("value.eventdate"));
            view.setTotal((Integer) source.get("value.total"));
            listAggView.setView(view);
            listAggView.setListViewId(listViewId);
            return listAggView;
        }
    
    }
    
    package com.ranker.app.mongo.customconverter.agglist.converter;
    
    import org.springframework.core.convert.converter.Converter;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DBObject;
    import com.ranker.app.mongo.domain.list.ListAggView;
    
    public class ListAggViewWriteConverter implements Converter<ListAggView, DBObject>
    {
        public DBObject convert(ListAggView source)
        {
            DBObject dbo = new BasicDBObject();
            dbo.put("_id.lid", source.getListViewId().getLid());
            dbo.put("_id.d", source.getListViewId().getEventDate());
            dbo.put("value.count", source.getView().getCount());
            dbo.put("value.total", source.getView().getTotal());
            dbo.put("value.mean", source.getView().getMean());
            dbo.put("value.eventdate", source.getView().getEventDate());
            return dbo;
        }
    }

    Service class:

    Code:
    @Service
    public class MDListViewAggService
    {
        @Autowired
        MongoTemplate m_mongoTemplate;
    
        public List<ListAggView> getAggView(Integer listId)
        {
            Query query = new Query();
            Criteria criteria = new Criteria().andOperator(Criteria.where("_id.lid").is(listId));
            query.addCriteria(criteria);
            List<ListAggView> listAggViews = m_mongoTemplate.find(query, ListAggView.class, "stats.daily");
            return listAggViews;
        }
    }
    Using spring data 1.1.0 version,

    If anybody find what am doing wrong here, please let me know

    thank you in advance

Working...
X