Search in sources :

Example 1 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 2 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 3 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)

Example 4 with EntityFieldMetaData

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

the class MappingBuilder method prepareReplaceAt.

public static BoundStatement prepareReplaceAt(Object id, Class<?> clazz, String propertyName, Object item, int idx, WriteOptions options, String keyspace, Session session) {
    EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
    EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
    Update update = QueryBuilder.update(keyspace, emeta.getTableName());
    if (fmeta.getType() == List.class) {
        update.with(QueryBuilder.setIdx(fmeta.getColumnName(), idx, item));
    }
    applyOptions(options, update, null);
    return prepareUpdate(id, emeta, update, session);
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 5 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)

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