Null For Id имени поля Встроенный объект из документа Mongo - PullRequest
0 голосов
/ 05 сентября 2018

У меня есть объект json в монго-документе, например:

{
    "_id" : "tuneeca_z_0915002",
    "variants" : [
        {
            "name" : "Z-0915002-Beige-S",
            "id" : "tuneeca_z_0915002_tuneeca_beige_base_s_size",
            "barCode" : "00017311"
        },
        {
            "name" : "Z-0915002-Beige-XS",
            "id" : "tuneeca_z_0915002_tuneeca_beige_base_xs_size",
            "barCode" : "00017312"
        },
        {
            "name" : "Z-0915002-Beige-M",
            "id" : "tuneeca_z_0915002_tuneeca_beige_base_m_size",
            "barCode" : "00017313"
        },
        {
            "name" : "Z-0915002-Beige-L",
            "id" : "tuneeca_z_0915002_tuneeca_beige_base_l_size",
            "barCode" : "00017314"
        },
        {
            "name" : "Z-0915002-Beige-XL",
            "id" : "tuneeca_z_0915002_tuneeca_beige_base_xl_size",
            "barCode" : "00017315"
        }
    ],
    "name" : "Z-0915002",
    "localSku" : "Z-0915002",
    "release" : {
        "id" : "tuneeca_premium",
        "name" : "Tuneeca Premium"
    }
}

и некоторые классы Java: ProductMongo.class

public class ProductMongo implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String SIMPLE = "SIMPLE";
    public static final String VARIED = "VARIED";

    private String id;
    private String barCode;
    private String localSku;
    private String name;
    private ProductRelease release;
    private List<Variant> variants;
    private BigDecimal cost;
    :
}

ProductRelease.class

public class ProductRelease {
    private String id;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "ProductRelease [id=" + id + ", name=" + name + "]";
    }
}

Variant.class и Value.class

public class Variant {

    public static class Value {
        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Value [value=" + value + "]";
        }

    }

    private String id;
    private String name;
    private String barCode;
    private Map<String, List<Value>> optionProperties;

    public String getId() {
        return id;
    }
    :
}

и затем в считыватель :

public class ProductReader extends MongoItemReader<ProductMongo> {

    public ProductReader(final MongoTemplate mongoTemplate) {
        super();

        setTemplate(mongoTemplate);
        setCollection("product");
        setQuery("{}");
        setFields("{_id: 1, type: 1, barCode: 1, localSku: 1, name: 1, 'release.id': 1, 'release.name': 1, "
                + "'variants.id': 1, 'variants.name': 1, 'variants.barCode': 1, 'variants.optionProperties': 1}");
        setTargetType(ProductMongo.class);
        setSort(new HashMap<String, Sort.Direction>() {{
            put("_id", Sort.Direction.DESC);
        }});
        setMaxItemCount(10);
    }

}

и я понятия не имею в процессоре (ItemProcessor) с:

  1. Нуль для варианта id
  2. Ноль для релиза id

Поле с именем id равно NULL для внедренного объекта или списка из документа.

как:

ProductMongo [id=tuneeca_z_0915002, barCode=null, localSku=Z-0915002, name=Z-0915002, release=ProductRelease [id=null, name=Tuneeca Premium], variants=[Variant [id=null, name=Z-0915002-Beige-S, barCode=00017311, optionProperties={base_size=[Value [value=base_s_size]], base_color=[Value [value=tuneeca_beige]]}], Variant [id=null, name=Z-0915002-Beige-XS, barCode=00017312, optionProperties={base_size=[Value [value=base_xs_size]], base_color=[Value [value=tuneeca_beige]]}], Variant [id=null, name=Z-0915002-Beige-M, barCode=00017313, optionProperties={base_size=[Value [value=base_m_size]], base_color=[Value [value=tuneeca_beige]]}], Variant [id=null, name=Z-0915002-Beige-L, barCode=00017314, optionProperties={base_size=[Value [value=base_l_size]], base_color=[Value [value=tuneeca_beige]]}], Variant [id=null, name=Z-0915002-Beige-XL, barCode=00017315, optionProperties={base_size=[Value [value=base_xl_size]], base_color=[Value [value=tuneeca_beige]]}]], cost=null, type=VARIED]

и последняя информация, это pom.xml:

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <!-- <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-jdbc</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

1 Ответ

0 голосов
/ 05 сентября 2018

Я решил, добавив поле Аннотация:

public class ProductRelease {
    @Field("id")
    private String id;
...