2.0.5 - MongoDB DataHandler and accessor needs to be modified to provide Map, collection support
The mongo instance does not let you store and load maps into a row like it should (and does for cassandra)
See the simple example:
package test.model;
import javax.persistence.*; import java.util.HashMap; import java.util.Map; import java.util.UUID;
@Entity @Table(schema = "test@testMongo", name = "Stuff") public class Stuff {
@Id
@Column(name = "stuffId")
private String stuffId;
@Column(name = "myMap")
private Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
public String getStuffId() {
return stuffId;
}
public void setStuffId(String stuffId) {
this.stuffId = stuffId;
}
public Map<Integer, Integer> getMyMap() {
return myMap;
}
public void setMyMap(Map<Integer, Integer> myMap) {
this.myMap = myMap;
}
@Override
public String toString() {
return "Stuff{" +
"stuffId='" + stuffId + '\'' +
", stuff=" + (myMap==null ? "" : myMap.size()) +
'}';
}
public static void main(String args[]) {
try {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("testMongo"); EntityManager em = emf.createEntityManager();
String id = UUID.randomUUID().toString();
Stuff stuff = new Stuff();
stuff.setStuffId(id);
stuff.getMyMap().put(99,1234);
stuff.getMyMap().put(12,67432);
System.out.println("pre save=" + stuff);
em.persist(stuff);
Stuff reloadStuff = em.find(Stuff.class, id);
System.out.println("post save and load=" + reloadStuff);
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I run I get this warning: pre save=Stuff{stuffId='1b45981b-7f78-4bc4-99fa-005968c1b4dc', stuff=2} ERROR: com.impetus.client.mongodb.MongoDBDataHandler - Error while Getting entity from Document. Details:java.lang.IllegalArgumentException: Can not set java.util.Map field test.model.Stuff.myMap to java.lang.String post save and load=Stuff{stuffId='1b45981b-7f78-4bc4-99fa-005968c1b4dc', stuff=}
I have tried Map<String,String> and I get the exact same error.
Also note that mongo shows this:
db.Stuff.find() { "_id" : "1b45981b-7f78-4bc4-99fa-005968c1b4dc", "stuffId" : "1b45981b-7f78-4bc4-99fa-005968c1b4dc", "myMap" : "{99=1234, 12=67432}" }
This should be something like this: "myMap" : [ {"99" : "1234"} , {"12" : "67432"} ]
Fixed with daad7fa29f81ed3593f98814dd95f03f8d5fcc4e
Support added.