2017-09-12 10 views
1

JDK 8、JodaTime 2.9.9およびOrientDBバージョン2.2.26を使用しています。OrientDBオブジェクトAPIのJoda DateTime

OrientDBオブジェクトAPIでDateTimeオブジェクトを使用する方法はありますか?

例クラス:

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. 

私はDateTimeの登録しようとした場合:

database.getEntityManager().registerEntityClass(DateTime) 

をそして私は再び実体を保存しよう:

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

それが最終クラスであるので、Javassistのが代理ではないので、私が得たことができます。

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

DateTimeではなくlongを格納するようにクラスを変更したくありません。 DateTimeのようなシリアライザとデシリアライザを実装して登録する方法はありますか?同様に、私のエンティティに干渉しないでしょうか?

+0

こんにちは、前に登録されていることが重要であり、あなたはコードを投稿できますか? Thx –

+0

@MichelaBonizzi完了、助けてください!どのバージョンを参照しているのか不明であったので、JDKとJodaのバージョンを追加しました.OrentDBのバージョンはすでにオリジナルのポストになっています。 – VitorCruz

+0

ちょうど興味深い:なぜあなたはJoda Timeを使用していますが、Java8のDate/Time APIを使用していませんか?サイトから:Java SE 8以降では、このプロジェクトを置き換えるJDKの中核部分であるjava.time(JSR-310)に移行するように求められます。 – MystyxMac

答えて

0

[OK]を、私はそれを行う方法(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は任意のエンティティクラスの登録、使用しているバージョン

関連する問題