我最近一直在使用 OrientDB 对象数据库,但遇到了一个奇怪的绊脚石。我想存储的一些对象有 BigIntegers 作为成员,出于某种原因,它们作为布尔值存储在数据库中。我尝试使用此处找到的示例代码。我有一个简短的示例和我的 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>