Search code examples
javamongodbpojomorphia

mongoDb morphia POJO property with read only access


I have created a POJO class in morphia to save data object to MongoDB ,

I have a property called 'unitPrice', i want to keep this as read only property, mean not allow to modify DB value once it save to mongo,
Is there a morphia annotation to do this or else,
Is it possible to bind @NotSaved annotation to the property at run time ?

This is my POJO

@Entity("items")  
public class Items  {
 private int id;
 private int sequence;
 private int unitPrice;  
}

Thanks
BR
Ero


Solution

  • I don't know of a way to bind @NotSaved at runtime, but I could think of a (hacky) workaround:

    • Set the attribute in the constructor (Morphia will still require the no-args constructor)
    • Don't provide a setter, just a getter for the attribute

    It ain't pretty, but it should get the job done.