Search in sources :

Example 6 with EntityFieldMetaData

use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.

the class MappingBuilder method getFromRow.

/**
     * Convert individual ResultSet Row into Entity instance
     */
public static <T> T getFromRow(Class<T> clazz, Row row) {
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
    T entity = null;
    Object primaryKey = null;
    Object partitionKey = null;
    // create PK
    try {
        entity = clazz.newInstance();
        PrimaryKeyMetadata pkmeta = entityMetadata.getPrimaryKeyMetadata();
        if (pkmeta.isCompound()) {
            EntityFieldMetaData pkField = pkmeta.getOwnField();
            primaryKey = pkField.getType().newInstance();
            pkField.setValue(entity, primaryKey);
            if (pkmeta.hasPartitionKey()) {
                PrimaryKeyMetadata partmeta = pkmeta.getPartitionKey();
                EntityFieldMetaData partField = partmeta.getOwnField();
                partitionKey = partField.getType().newInstance();
                partField.setValue(primaryKey, partitionKey);
            }
        }
    } catch (Exception e) {
    // skip error to support any-2-any
    }
    // set properties' values
    for (EntityFieldMetaData field : entityMetadata.getFields()) {
        Object value = getValueFromRow(row, field);
        try {
            if (value != null) {
                if (field.isPartition()) {
                    field.setValue(partitionKey, value);
                } else if (field.isPrimary()) {
                    field.setValue(primaryKey, value);
                } else {
                    field.setValue(entity, value);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return entity;
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) PrimaryKeyMetadata(com.datastax.driver.mapping.meta.PrimaryKeyMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with EntityFieldMetaData

use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.

the class MappingBuilder method buildInsert.

/**
     * Statement to persist an entity in Cassandra
     * 
     * @param entity to be inserted
     * @return com.datastax.driver.core.BoundStatement
     */
public static <E> BuiltStatement buildInsert(E entity, WriteOptions options, String keyspace) {
    Class<?> clazz = entity.getClass();
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
    String table = entityMetadata.getTableName();
    List<EntityFieldMetaData> fields = entityMetadata.getFields();
    List<String> pkCols = entityMetadata.getPkColumns();
    List<Object> pkVals = entityMetadata.getEntityPKValues(entity);
    String[] columns = new String[fields.size()];
    Object[] values = new Object[fields.size()];
    EntityFieldMetaData verField = null;
    Object newVersion = null;
    Object oldVersion = null;
    // increment and set @Version field
    if (entityMetadata.hasVersion()) {
        verField = entityMetadata.getVersionField();
        oldVersion = verField.getValue(entity);
        newVersion = incVersion(oldVersion);
        verField.setValue(entity, newVersion);
    }
    for (int i = 0; i < fields.size(); i++) {
        EntityFieldMetaData f = fields.get(i);
        String colName = f.getColumnName();
        Object colVal = null;
        if (pkCols.contains(colName)) {
            int idx = pkCols.indexOf(colName);
            colVal = pkVals.get(idx);
            if (colVal == null && f.isAutoGenerate()) {
                if (f.getDataType() == DataType.Name.TIMEUUID) {
                    colVal = QueryBuilder.fcall("now");
                } else if (f.getDataType() == DataType.Name.UUID) {
                    colVal = QueryBuilder.fcall("uuid");
                }
            }
        } else {
            colVal = f.getValue(entity);
        }
        columns[i] = colName;
        if (f.equals(verField)) {
            values[i] = newVersion;
        } else {
            values[i] = colVal;
        }
    }
    Insert insert = insertInto(keyspace, table).values(columns, values);
    if (verField != null) {
        insert.ifNotExists();
    }
    applyOptions(options, insert, entityMetadata);
    return insert;
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 8 with EntityFieldMetaData

use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.

the class EntityTypeParserTest method testGetEntityMetadataOverrideCollectionType.

@Test
public void testGetEntityMetadataOverrideCollectionType() {
    EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityWithCollectionsOverride.class);
    assertEquals(4, meta.getFields().size());
    EntityFieldMetaData fd = meta.getFieldMetadata("rates");
    assertEquals(TreeMap.class, fd.getCollectionType());
    fd = meta.getFieldMetadata("refs");
    assertEquals(TreeSet.class, fd.getCollectionType());
    fd = meta.getFieldMetadata("trades");
    assertEquals(LinkedList.class, fd.getCollectionType());
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Test(org.junit.Test)

Example 9 with EntityFieldMetaData

use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.

the class EntityTypeParserTest method testGetEntityMetadataOverrideDataType.

@Test
public void testGetEntityMetadataOverrideDataType() {
    EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityOverrideDataType.class);
    assertEquals(2, meta.getFields().size());
    EntityFieldMetaData fd = meta.getFieldMetadata("uid");
    assertEquals(DataType.Name.TIMEUUID, fd.getDataType());
    fd = meta.getFieldMetadata("name");
    assertEquals(DataType.Name.VARCHAR, fd.getDataType());
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Test(org.junit.Test)

Example 10 with EntityFieldMetaData

use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.

the class MappingSessionTest method getByQueryTest.

@Test
public void getByQueryTest() throws Exception {
    for (int i = 0; i < 3; i++) {
        EntityWithIndexes obj = new EntityWithIndexes();
        obj.setCount(100);
        obj.setEmail("email@test");
        obj.setName("test" + i);
        obj.setTimeStamp(new Date());
        obj.setUuid(UUID.randomUUID());
        target.save(obj);
    }
    EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
    EntityFieldMetaData fdata = emeta.getFieldMetadata("email");
    Statement query = QueryBuilder.select().all().from(keyspace, emeta.getTableName()).where(eq(fdata.getColumnName(), "email@test"));
    List<EntityWithIndexes> items = target.getByQuery(EntityWithIndexes.class, query);
    assertEquals(3, items.size());
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Aggregations

EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)18 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)13 PrimaryKeyMetadata (com.datastax.driver.mapping.meta.PrimaryKeyMetadata)2 ExecutionException (java.util.concurrent.ExecutionException)2 Test (org.junit.Test)2 Cluster (com.datastax.driver.core.Cluster)1 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 DataType (com.datastax.driver.core.DataType)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 TableMetadata (com.datastax.driver.core.TableMetadata)1 Static (com.datastax.driver.mapping.annotation.Static)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 EmbeddedId (javax.persistence.EmbeddedId)1 GeneratedValue (javax.persistence.GeneratedValue)1 Transient (javax.persistence.Transient)1 Version (javax.persistence.Version)1