I have been trying to register my own converter for reads and unfortunately it is not getting used. I can see it getting registered but it never actually calls the reader. Here are the corresponding files:
Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <mongo:repositories base-package="com.tot.model.repositories" /> <mongo:mongo host="localhost" port="27017" /> <mongo:db-factory dbname="database" mongo-ref="mongo" /> <mongo:mapping-converter id="mappingConverter" base-package="com.tot.model"> <mongo:custom-converters> <mongo:converter ref="readConverter" /> </mongo:custom-converters> </mongo:mapping-converter> <bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator" /> <!-- To translate any MongoExceptions thrown in @Repository annotated classes --> <bean id="mongo" class="org.springframework.data.document.mongodb.MongoFactoryBean"> <property name="host" value="localhost" /> <property name="port" value="27017" /> </bean> <bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean"> <property name="template" ref="mongoTemplate" /> <property name="repositoryInterface" value="com.tot.model.dao.repository.FeedItemDaoRepository" /> </bean> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mongoConverter" ref="mappingConverter" /> </bean> <bean id="readConverter" class="com.tot.model.dao.repository.util.UserPointsReadConverter" /> <bean class="org.springframework.data.document.mongodb.mapping.event.LoggingEventListener" /> </beans>
Code:package com.tot.model.dao.repository.util; import org.springframework.core.convert.converter.Converter; import com.mongodb.DBObject; import com.tot.model.UserPoints; public class UserPointsReadConverter implements Converter<DBObject, UserPoints> { @Override public UserPoints convert(DBObject source) { UserPoints userPoints = new UserPoints(); userPoints.setId((Long) source.get("_id")); userPoints.setPoints((Integer) source.get("value.points")); return userPoints; } }Code:package com.tot.model; import javax.persistence.Transient; import org.springframework.data.document.mongodb.index.Indexed; import org.springframework.data.document.mongodb.mapping.Document; import org.springframework.data.annotation.Id; @Document public class UserPoints { @Id private Long id; @Transient private User user; @Indexed private Integer points; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getPoints() { return points; } public void setPoints(Integer points) { this.points = points; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }I can see this data coming back but my reader never gets called. Spring just does it's best to convert it.Code:INFO [2011-06-03 13:50:05,228] [TP-Processor3] (LoggingEventListener.java:50) - onAfterLoad: { "_id" : 13849 , "value" : { "count" : 1402.0 , "points" : 1498.0}} INFO [2011-06-03 13:50:05,229] [TP-Processor3] (LoggingEventListener.java:55) - onAfterConvert: { "_id" : 13849 , "value" : { "count" : 1402.0 , "points" : 1498.0}}, com.tot.model.UserPoints@7c02a5e1


Reply With Quote
