Question

We are using flyweight pattern for some objects in our system. Those objects are also saved in database (mongodb). After loading the object back from db using Spring Data, there is no easy way to replace the object constructed by Spring Data with the object in the flyweight cache. For Java deserialization, there is readResolve() method. I wonder if Spring Data can add something similar to support this use case.

Is there any solutions with the current Spring Data implementation (1.4.1 release)?

Was it helpful?

Solution 2

Thanks @Pelit_Mamani for some suggestions. I did try to implement a converter and set it in mongo mapping converter and it seems to work. It works even when the object is embedded in other object.

    <mongo:mapping-converter id="mappingConverter" base-package="com.mytest.domain" db-factory-ref="mongoDbFactory" disable-validation="true">
    <mongo:custom-converters>
        <mongo:converter>
            <bean class="com.mytest.repo.converter.MyReadConverter" />
        </mongo:converter>
    </mongo:custom-converters>
</mongo:mapping-converter>

And the converter class:

public class MyReadConverter implements Converter<DBObject, MyObject>

OTHER TIPS

They must have something otherwise enums wouldn't work either... I'd consider custom converters, e.g. here (look for the last section with PersonReadConverer): http://docs.spring.io/spring-data/mongodb/docs/1.4.x/reference/html/mapping-chapter.html

I just hope it works when Person is a nested field inside another class - didn't get a chance to test it .

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top