0

I'm using MongoRepository for saving/retrieving an object in MongoDB. I have an object with properties defined as such:

@Document
public class ConfigurationItem {

    String s1;

    @Transient
    String s2;
}

The repository will save/retrieve s1 as expected, but leave s2 alone since it's defined transient.

Now the problem: where do I call my "init transients" method which can put the right value into s2? I want this to be called after every instantiation of an object of type ConfigurationItem, but the constructor is too early, s1 does not have its value set yet, and s2's value depends on s1.

Is there a "post construction" method which I can override?

TheZuck
  • 3,513
  • 2
  • 29
  • 36

1 Answers1

1

The @PersistenceConstructor annotation should allow you to set s2 based on the value of s1 when the object is hydrated from the database.

jmikola
  • 6,892
  • 1
  • 31
  • 61
  • Thanks for your answer, it would definitely work, however, I prefer not to use a parameterized constructor as that makes room for error when adding fields. I prefer to have the fields auto-injected. – TheZuck Aug 21 '12 at 10:17
  • Would a persistence constructor disable automatic hydration of mapped fields? My understanding from the documentation was that it was triggered in addition to the default behavior, but I haven't worked with the library. – jmikola Aug 21 '12 at 14:16
  • hmm I don't know if it would disable it, however, since the constructor must be called before any variables can be set by a setter method externally (the object must be instantiated before used), I would not be able to use any of the hydrated data during construction time, as it would only come afterwards... – TheZuck Aug 22 '12 at 12:23