OrientDB自定义类型存储为布尔值

时间:2013-11-07 17:28:58

标签: orientdb

我最近一直在使用OrientDB Object数据库,但我遇到了一个奇怪的绊脚石。我想要存储的一些对象具有BigIntegers作为成员,并且由于某种原因它们作为布尔存储在数据库中。我尝试使用找到的示例代码here。我有一个简短的示例和我的Maven依赖项以在下面演示,但这是我的两个问题:

为了让它发挥作用,为什么这不起作用?

为了更好地理解,它是如何产生布尔值的?

示例代码:

package test;

import java.math.BigInteger;
import java.util.List;

import com.orientechnologies.orient.core.serialization.serializer.object.OObjectSerializer;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.object.serialization.OObjectSerializerContext;
import com.orientechnologies.orient.object.serialization.OObjectSerializerHelper;

public class HashmapIterationTest {
    public static void main(String[] args) throws Exception{
        Runner r = new Runner();
        r.run();
    }

public static class Runner{

    private OObjectDatabaseTx db;

    public void run() throws Exception{
        db = new OObjectDatabaseTx("plocal:c:/testodb");
        if(db.exists()){
            db.open("admin", "admin");
        }else{
            db.create();
        }

        //Hack for BigInteger
        OObjectSerializerContext serializerContext = new OObjectSerializerContext();
        serializerContext.bind(new OObjectSerializer<BigInteger, Long>() {
            public Long serializeFieldValue(Class<?> itype,  BigInteger iFieldValue) {
                return iFieldValue.longValue();
            }
            public  BigInteger unserializeFieldValue(Class<?> itype, Long iFieldValue) {
                return BigInteger.valueOf(iFieldValue);
            }
        });
        OObjectSerializerHelper.bindSerializerContext(null, serializerContext);

        db.getEntityManager().registerEntityClass(Parent.class);

        db.save(new Parent(5));

        List<Parent> result = db.query(new OSQLSynchQuery<Parent>("select from parent"));
        for(Parent p: result){
            System.out.println("Parent: " + p.getBigInt());
        }
    }
}

public static class Parent{
    BigInteger bigInt;
    public Parent(){
    }
    public Parent(int test){
        this.bigInt = BigInteger.valueOf(test);
    }
    public void setBigInt(BigInteger bigInt){
        this.bigInt = bigInt;
    }
    public BigInteger getBigInt(){
        return bigInt;
    }
    public String toString(){
        return "Parent: " + getBigInt();
    }
}

}

版本信息的Maven依赖项:

<dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orient-commons</artifactId>
        <version>1.5.1</version>
        <type>bundle</type>
    </dependency>
    <dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orientdb-core</artifactId>
        <version>1.5.1</version>
        <type>bundle</type>
    </dependency>
    <dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orientdb-object</artifactId>
        <version>1.5.1</version>
        <type>bundle</type>
    </dependency>

1 个答案:

答案 0 :(得分:0)

我与OrientDB Google小组的那些人取得了联系,他们为我回答了这个问题,所以我只是为了后代而留在这里。

我正在阅读的文档不正确。不支持BigInteger作为自定义数据类型。 Google Groups Discussion