I'm getting the following exception when attempting to save a simple POJO with a map field.
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.orientechnologies.orient.core.db.record.OIdentifiable
Here's my code:
package com.example.test;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Main {
static class Entity {
private Map map;
public Entity() {
}
public Entity(Map map) {
this.map = map;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
private static void doIt() {
String dbUser = "root";
String dbPass = "root";
String dbUrl = "remote:localhost:2424/sandbox";
try {
OServerAdmin serverAdmin = new OServerAdmin(dbUrl);
serverAdmin.connect(dbUser, dbPass);
if (serverAdmin.existsDatabase("plocal")) {
serverAdmin.dropDatabase("plocal");
}
serverAdmin.createDatabase("document", "plocal");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
ODatabaseDocumentTx databaseDocumentTx = new ODatabaseDocumentTx(dbUrl);
databaseDocumentTx.open(dbUser, dbPass);
OObjectDatabaseTx objectDatabaseTx = new OObjectDatabaseTx(databaseDocumentTx);
objectDatabaseTx.getEntityManager().registerEntityClass(Entity.class);
Map map = new HashMap();
map.put("a", "a");
Entity entity = objectDatabaseTx.save(new Entity(map)); //<-- EXCEPTION THROWN HERE!!!
}
public static void main(String[] args) {
doIt();
}
}
Full stack trace:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.orientechnologies.orient.core.db.record.OIdentifiable
at com.orientechnologies.orient.core.db.record.ORecordLazyMap.put(ORecordLazyMap.java:41)
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.multiValueToStream(OObjectEntitySerializer.java:1398)
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.typeToStream(OObjectEntitySerializer.java:805)
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.toStream(OObjectEntitySerializer.java:1216)
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.serializeObject(OObjectEntitySerializer.java:144)
at com.orientechnologies.orient.object.db.OObjectDatabaseTx.save(OObjectDatabaseTx.java:454)
at com.orientechnologies.orient.object.db.OObjectDatabaseTx.save(OObjectDatabaseTx.java:399)
at com.example.test.Main.doIt(Main.java:56)
at com.example.test.Main.main(Main.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Any guidance as to what is going on would greatly be appreciated!
After some digging around I'm going to answer my own question. I can make my code work by explicitly declaring the Entity.map field as OType.EMBEDDEDMAP. See below:
...
objectDatabaseTx.getEntityManager().registerEntityClass(Entity.class);
OClass oClass = objectDatabaseTx.getMetadata().getSchema().getClass(Entity.class);
oClass.createProperty("map", OType.EMBEDDEDMAP);
...
However, I ultimately need my map to contain any value (ie: Map<String, Object>
). But I can't because there's an open bug for that ...
https://github.com/orientechnologies/orientdb/issues/3063
So that's it folks...