从Scala访问OrientDB 1.7-RC1,在尝试使用embeddedset自定义类型的对象保存ODocument时,我得到一个异常,说明相应的字段是不兼容的类型。我需要帮助弄清楚我做错了什么。
创建/保存ODocument的代码如下所示,它表示一个具有嵌入式Dependency对象的Package:
val myHashSet = new util.HashSet[models.Dependency]()
myHashSet.add(new models.Dependency("mydep", "1.0"))
val myVersion = new ODocument("Version")
.field("id", "myversion")
.field("dependencies", myHashSet)
myVersion.save()
这会导致抛出以下异常:
com.orientechnologies.orient.core.exception.OValidationException: The field 'Version.dependencies' has been declared as EMBEDDEDSET but an incompatible type is used. Value: Dependency(mydep,1.0)
at com.orientechnologies.orient.core.record.ORecordSchemaAwareAbstract.validateEmbedded(ORecordSchemaAwareAbstract.java:429) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
at com.orientechnologies.orient.core.record.ORecordSchemaAwareAbstract.validateField(ORecordSchemaAwareAbstract.java:249) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
at com.orientechnologies.orient.core.record.ORecordSchemaAwareAbstract.validate(ORecordSchemaAwareAbstract.java:71) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
at com.orientechnologies.orient.core.record.impl.ODocument.save(ODocument.java:1357) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
at com.orientechnologies.orient.core.record.impl.ODocument.save(ODocument.java:1347) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
at com.orientechnologies.orient.core.record.impl.ODocument.save(ODocument.java:1336) ~[orientdb-core-1.7-rc1.jar:1.7-rc1]
我已经在数据库的架构中声明了Version.dependencies嵌入式集,如下所示:
create property Version.dependencies embeddedset Dependency
Dependency类定义如下所示(它实现了OSerializableStream,因为这可能是序列化嵌入对象所必需的):
case class Dependency(var id: String, var specifier: String) extends OSerializableStream {
def this() = this(null, null)
override def toStream: Array[Byte] = {
val bytes: Array[Byte] = Json.prettyPrint(Json.toJson(this)).getBytes
bytes
}
override def fromStream(iStream: Array[Byte]): OSerializableStream = {
val json = Json.parse(iStream)
val dependency = Json.fromJson[Dependency](json).get
dependency
}
}
object Dependency {
implicit val versionFormat = Json.format[Dependency]
}
我在这里做错了什么?
答案 0 :(得分:0)
嵌入对象必须是ODocument类型,它不能帮助使Dependency实现OSerializable。以下代码显示了:
val myHashSet = new java.util.HashSet[ODocument]()
myHashSet.add(new ODocument("Dependency").field("id", "mydep").field("specifier", "1.0"))
val myVersion = new ODocument("Version")
.field("id", "myversion")
.field("dependencies", myHashSet)
myVersion.save()