简体   繁体   中英

How do I embed documents in a Map using Spring Data MongoDB?

I have persistent class similar to the following:

public class Invoice {

    private Map<String, PurchasedItem> itemMap;

}

The PurchasedItem class looks like:

public class PurchasedItem {

    private String name;

    private int quantity;

}

Let's just say, I have good reasons not to store PurchasedItem objects into their own collection, and I want to store each of them under their respective parent Invoice objects. How do I annotate my classes so that Spring Data MongoDB automatically maps the itemMap correctly to something like:

"itemMap": {
    "tomato": {
        "name": "tomato",
        "quantity": 5
    }
}

Annotate Invoice with @Document (in spring-mongo)

Also, an id will be required with invoice.

It will store all the fields of Invoice in a JSON format, including the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM