I have tried to do something like this:
package org.dnylabs.kosh.data;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
@Entity
public class Temp {
@Id String _id;
List<Map<String,Object>> strings;
public Temp(){
strings=new LinkedList<Map<String,Object>>();
}
public static void main(String []args) throws UnknownHostException, MongoException{
Mongo mongo=null;
Morphia morphia=null;
Datastore ds=null;
mongo = new Mongo();
morphia = new Morphia();
morphia.map(Temp.class);
ds = morphia.createDatastore(mongo, "test");
Temp t = new Temp();
t._id ="hi";
Map<String, Object> m = new HashMap<String, Object>();
m.put("Hi","1");
m.put("Hi2",2);
t.strings.add(m);
ds.save(t);
t=ds.get(t);
ds.ensureIndexes();
}
}
When I try to do a findAll(9
operation I get this exception:
Caused by: java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: Embedded element isn't a DBObject! How can it be that is a class java.lang.String
at org.mongodb.morphia.mapping. here`dedMapper.fromDBObject(EmbeddedMapper.java:172)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:602)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:559)
at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:256)
at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:203)
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:144)
... 16 more
After numerous attempts I have found that the problem is the grafted map.
Can anyone help me understand where I'm wrong? The statement seems correct.