java - OrientDB 对象 API 中的 Joda DateTime

标签 java orientdb

我使用的是 JDK 8、JodaTime 2.9.9 和 OrientDB 版本 2.2.26。

有没有办法将 DateTime 对象与 OrientDB 对象 API 一起使用?

示例类:

class Entity {

    private DateTime date;

    public Entity(DateTime date){
        this.date = date
    }

    public DateTime getDate(){
        return date;
    }

    public void setDate(DateTime newDate){
        this.date = newDate;
    }
}

在 OrientDB 中注册:

database.getEntityManager().registerEntityClass(Entity)

如果我尝试保存它:

database.save(new Entity(DateTime.now()))

然后我得到:

com.orientechnologies.orient.core.exception.OSerializationException: 
Linked type [class org.joda.time.DateTime:2017–09–12T11:50:25.709–03:00] 
cannot be serialized because is not part of registered entities.

如果我尝试注册日期时间:

database.getEntityManager().registerEntityClass(DateTime)

我尝试再次保存实体:

database.save(new Entity(DateTime.now()))

由于它是一个final类,javassist无法代理它,所以我得到了:

java.lang.RuntimeException: org.joda.time.DateTime is final

我不想更改我的类来存储 long 而不是 DateTime。有没有办法为 DateTime 实现和注册某种序列化器和反序列化器,或者类似地不会干扰我的实体?

最佳答案

好的,我知道如何做到这一点(Groovy 中的代码):

    def orientDbServer = OServerMain.create()
    System.setProperty("ORIENTDB_HOME", new File("").getAbsolutePath());
    orientDbServer.startup(new OServerConfiguration().with { cfg ->
        location = "memory"
        network = new OServerNetworkConfiguration(this)
        users = [new OServerUserConfiguration(name: "root", 
                                              password: "root",
                                              resources: "*")] as OServerUserConfiguration[]
        cfg
    })
    orientDbServer.activate()

    addShutdownHook {
        orientDbServer.shutdown()
    }

    new OServerAdmin("localhost").connect("root", "root")
                                 .createDatabase("test", "document", "memory").close()
    OObjectDatabaseTx database = 
                    new OObjectDatabaseTx("memory:localhost/test").open("admin", "admin")

    OObjectSerializerContext serializerContext = new OObjectSerializerContext();
    serializerContext.bind(new OObjectSerializer<DateTime, Long>() {
        @Override
        Object serializeFieldValue(Class<?> iClass, DateTime iFieldValue) {
            return iFieldValue.getMillis()
        }

        @Override
        Object unserializeFieldValue(Class<?> iClass, Long iFieldValue) {
            return new DateTime(iFieldValue)
        }
    }, database)

    OObjectSerializerHelper.bindSerializerContext(null, serializerContext)

重要的是,在任何实体类注册之前注册serializerContext

关于java - OrientDB 对象 API 中的 Joda DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46179736/

相关文章:

orientdb - OrientJS:如何从查询中获取标准 JSON(序列化)

java - RDF4J:EC2 上出现 "Failed to get server protocol"错误,但本地没有

java - 将对列表转换为分组对列表

java - 是否可以为 simple_list_item1 自定义 ListView 项的大小

java - 在同一虚拟机内切换 WAL 使用?

java - OrientDB无法使用java远程获取图形实例

java - Java中创建硬链接(hard link)和删除文件的原子操作

java - 从 ArrayAdapter 中删除项目

java - 尝试与 OrientDB 合作

java - Orientdb无法在docker容器内插入数据