Search code examples
springspring-dataspring-data-couchbase

Spring Data Couchbase: How to rename fields from nested POJOs?


Doc says that the @Field annotation can be used to rename a field in an entity. What about fields from nested POJOs that are technically not entities themselves? Consider the following hypothetical example.

@Document
public class Person {
    @Id
    private String ssn;
    @Field
    private String name;
    @Field
    private Address address;

    static class Address {
        // how to rename this field to line1?
        private String street;
    }
}

Solution

  • To answer your question specifically, you can use @Field("line1") for street in Address.

    I have something like this in my project and it works fine (see descriptions)

    Class 1

    @Document
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class HotelInfo {
        @Field("hotel_type") @JsonProperty("hotel_type")
        public String hotelType;
        @Field @JsonProperty("images")
        public List<Image> images = new ArrayList<Image>();
        @Field @JsonProperty("regions")
        public List<String> regions = new ArrayList<String>();
        @Field @JsonProperty("themes")
        public List<String> themes = new ArrayList<String>();
        @Field @JsonProperty("facilities")
        public List<String> facilities;
        @Field @JsonProperty("descriptions")
        public Descriptions descriptions;
    }
    

    Class 2

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Descriptions {
        @Field("hotel_information") @JsonProperty("hotel_information")
        public String hotelInformation;
    }