Playorm java.lang.Integer无法强制转换为java.math.BigInteger

时间:2013-09-04 12:55:30

标签: java playorm

我尝试构建基于A basic ad hoc query example 的基本即席查询示例。

但是,我无法插入Integer值。我认为这是因为这段代码:

private Class getStorageTypeAsClass() {
    switch (getStorageType())
    {
        case STRING:
            return String.class;
        case INTEGER:
            return BigInteger.class;
    }
}

我的代码:

package playorm;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import com.alvazan.orm.api.base.Bootstrap;
import com.alvazan.orm.api.base.DbTypeEnum;
import com.alvazan.orm.api.base.NoSqlEntityManager;
import com.alvazan.orm.api.base.NoSqlEntityManagerFactory;
import com.alvazan.orm.api.z3api.NoSqlTypedSession;
import com.alvazan.orm.api.z8spi.KeyValue;
import com.alvazan.orm.api.z8spi.meta.DboColumnCommonMeta;
import com.alvazan.orm.api.z8spi.meta.DboColumnIdMeta;
import com.alvazan.orm.api.z8spi.meta.DboDatabaseMeta;
import com.alvazan.orm.api.z8spi.meta.DboTableMeta;
import com.alvazan.orm.api.z8spi.meta.TypedColumn;
import com.alvazan.orm.api.z8spi.meta.TypedRow;

public class PlayOrm {
    private static NoSqlEntityManager mgr;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {

        Map properties = new HashMap();
        properties.put(Bootstrap.AUTO_CREATE_KEY, "create");

        String clusterName = "Test Cluster";
        String seeds = "localhost:9160";
        String keyspace = "nosql";

        Bootstrap.createAndAddBestCassandraConfiguration(properties, clusterName, keyspace, seeds);
        NoSqlEntityManagerFactory factory = Bootstrap.create(DbTypeEnum.CASSANDRA, properties,null,null);
        mgr = factory.createEntityManager();
        DboDatabaseMeta metaDb = mgr.find(DboDatabaseMeta.class,
                DboDatabaseMeta.META_DB_ROWKEY);
        DboTableMeta meta = new DboTableMeta();
        meta.setup(null, "MyEntity", false);
        metaDb.addMetaClassDbo(meta);

        DboColumnIdMeta idMeta = new DboColumnIdMeta();
        idMeta.setup(meta, "idField", String.class, true);
        mgr.put(idMeta);

        DboColumnCommonMeta fieldDbo = new DboColumnCommonMeta();
        fieldDbo.setup(meta, "firstname", String.class, true, false);
        mgr.put(fieldDbo);
        DboColumnCommonMeta fieldDbo2 = new DboColumnCommonMeta();
        fieldDbo2.setup(meta, "lastname", Integer.class, true, false);
        mgr.put(fieldDbo2);

        mgr.put(meta);
        mgr.flush();

        NoSqlTypedSession session = mgr.getTypedSession();
        for (int i = 0; i < 10000; i++) {
            TypedRow typedRow = session.createTypedRow("MyEntity");
            typedRow.addColumn("firstname", "harry");
                        //**THE PROBLEM**
            typedRow.addColumn("lastname", new Integer(i));
            typedRow.setRowKey(UUID.randomUUID().toString());

            session.put("MyEntity", typedRow);

            if(i > 1000 && (i % 10) == 0 ){
                session.flush();
                mgr.clear();
            }

        }
        session.flush();

}

没有选项可以返回Integer.class。因此,它无法构建整数转换器:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.alvazan.orm.impl.meta.data.NoSqlProxyImpl.invoke(NoSqlProxyImpl.java:96)
    at com.alvazan.orm.api.z8spi.meta.DboColumnCommonMeta_$$_javassist_1.convertToStorage2(DboColumnCommonMeta_$$_javassist_1.java)
    at com.alvazan.orm.api.z8spi.meta.TypedRow.addColumn(TypedRow.java:99)
    at playorm.PlayOrm.main(PlayOrm.java:62)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger
    at com.alvazan.orm.api.z8spi.conv.Converters$BigIntegerConverter.convertToNoSqlImpl(Converters.java:89)
    at com.alvazan.orm.api.z8spi.conv.Converters$BaseConverter.convertToNoSql(Converters.java:138)
    at com.alvazan.orm.api.z8spi.meta.DboColumnMeta.convertToStorage2(DboColumnMeta.java:167)
    at com.alvazan.orm.api.z8spi.meta.DboColumnCommonMeta_$$_javassist_1._d8convertToStorage2(DboColumnCommonMeta_$$_javassist_1.java)
    ... 8 more

1 个答案:

答案 0 :(得分:0)

TypedRow是一个特殊的api,现在允许BigInteger,BigDecimal和String类型......但是这些类型在存储时存储为可能的最低字节,因此int值10存储为一个字节。

出于好奇,为什么不使用带@NoSqlEntity注释的MyEntity.java类来代替?在您的情况下,使用POJO似乎更有益。

MyEntity.java可以有任何基本类型,int,double等等。

我们认为您使用的是实体,因为大多数实体使用实体来加快开发速度。

后, 迪安