Saving and receiving Map Cards with Morphine and Mondomb

I would like to be able to save and retrieve, among other things, a map card in the MongoDB collection. I use Java to access MongoDB through Morphia.

The example I use below is a collection containing documents detailing the owners of various cars. In this example, the number of vehicles of a particular make and model is stored on a map card

Most properties work without any problems, but for the case when the property is a display mapping defined as follows:

@Property("vehicles")
private Map<String, Map<String, Integer> vehicles = new HashMap<String, HashMap<String, Integer>>();

The object is created (some values ​​are inserted into the map) and stored in the Mongo database, as you would expect:

"vehicles" : {
    "FORD" : {
        "FIESTA" : 1
    },
    "TOYOTA" : {
        "COROLLA" : 1,                  
        "PRIUS": 1
    },
    "BMW" : {
        "SLK" : 1
    }
}

, java- ( MongoDB , )) ...

Query<Owner> q = ds.find(Owner.class);    
System.out.println(q.countAll());
Iterable<Owner> i = q.fetch();
for (Owner o : i) {
    System.out.println(o);
}

... q.fetch().

, :)

+5
2

, Map ( ) , Morphia HashMap , . NullPointerException.

, ( ) .

  • @Property @Embedded.
  • HashMap

    @Embedded("vehicles")
    private HashMap<String, HashMap<String, Integer>> vehicles = new HashMap<String, HashMap<String, Integer>>();
    

, ... @Property @Embedded HashMap.

+7

private HashMap<String, HashMap<String, OwnDataType>> vehicles = new HashMap<String, HashMap<String, OwnDataType>>();

; , , , , OwnDataType , OwnDataType :

private HashMap<String, OwnDataTypes> vehicles = new HashMap<String, OwnDataTypes>();

.

+2

All Articles