Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: why Morphia doesn't run Converters for Mapped Fields? #1231

Closed
tnymlr opened this issue Feb 20, 2018 · 1 comment
Closed

Question: why Morphia doesn't run Converters for Mapped Fields? #1231

tnymlr opened this issue Feb 20, 2018 · 1 comment
Labels

Comments

@tnymlr
Copy link

tnymlr commented Feb 20, 2018

As of now, Morphia scans entity classes for "interesting annotations". Once it finds a field with an "interesting annotation" it puts it into "persistence fields". Once I apply "ensureIndexes" to datastore all of the persistence fields classes that hasn't been copied into "mapped classes" is copied into that collection.

Then, when mapping happens, Morphia checks if a field class is member of that collection and doesn't apply converters if it is.

Is there any particular reason for that? Can it be changed that Morphia applies converters for mapped classes also? It would be really neat to be able to customise how model objects are built from DBObjects for certain cases.

My use case is that I have immutable class with private constructor as part of api and I want to be able to store and retrieve objects of that class with Mongodb. While I am able to store it - I'm unable to load it as Morphia's default behaviour is to use default constructor and I don't have one.

I was able to override ObjectFactory with my custom class but it only controls the object instantiation phase, Morphia still writes values into my private final fields which I would like to avoid using converters.

@evanchooly
Copy link
Member

Updating the mapping code in 1.x to do constructor injection of values would an enormous amount of work, I think. The mapping code in 2.x has been rewritten and is much more suitable for such an addition and is, in fact, on the roadmap albeit by a very old bug (#581).

I know this answer is less than ideal but it's why I worked so hard to refactor all that for 2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants