Marcin K.
Marcin K.

Reputation: 843

How to make MongoRepository read both old and new version of database entity after adding new field

I have 2 services. One writes data to MongoDB database and the other reads these data. I have a shared dependency that contains definitions of the database objects so they are consistent between projects. However the project that WRITES to the database for some reasons is delivered with a few weeks delay. The entity in question needs to get a new field - an int. I want the reading service code to be able to read the new entity, but also to be able to read the old entity and assign a default hardcoded value to the new field, until the new entity definition reaches the writing service and therefor the database. I am using MongoRepository class to read the data from the database. I am the owner of both projects and the dependency. So far I had 2 ideas:

  1. Temporarily create an extra constructor for the entity that would take 1 less argument and hope that MongoRepository will try to use it. However I got "constructor cannot be applied to given types" exception here.
  2. Use Integer instead of int as the entity field, so I get null value in my constructor instead of exception. I then check for null and put default value in such case. This seems to work but I am worried that it can cause problems after I change the entity's field back to "int", as maybe data written as "Integer" is stored differently than "int".

What is the best way to achieve my goal? Will there be a problem with solution #2?

Upvotes: 1

Views: 17

Answers (0)

Related Questions