Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any Spring Annotation to Set Default value for a Field (Mongo)?

Is there any Spring Annotation to Set Default value for a Field (Mongo) ?

like image 954
Gaurav Agrawal Avatar asked Dec 27 '16 11:12

Gaurav Agrawal


2 Answers

No need for spring annotations, this should do the trick:

     import org.springframework.data.mongodb.core.mapping.Document;
     import org.springframework.data.mongodb.core.mapping.Field;


    @Document
    public class Doc {

    @Field
    private String field = "CustomDefaultValue"; 

    }
like image 192
felix Avatar answered Sep 29 '22 09:09

felix


You have to tell your Builder to use default value for some field using @Builder.Default(annotation).

Like this,

    @Builder
    @Document
    public class Document {

    @Builder.Default
    private String field = "any_value"; 

    }
like image 36
Gaurav Raghav Avatar answered Sep 29 '22 07:09

Gaurav Raghav