Search in sources :

Example 1 with PrimaryKeyMetadata

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

the class EntityTypeParser method parsePropertyLevelMetadata.

private static EntityTypeMetadata parsePropertyLevelMetadata(Class<?> clazz, EntityTypeMetadata result, PrimaryKeyMetadata pkmeta, boolean isPartitionKey) {
    Field[] fields = clazz.getDeclaredFields();
    Method[] methods = clazz.getDeclaredMethods();
    for (Field f : fields) {
        boolean isOwnField = false;
        PrimaryKeyMetadata pkm = null;
        // for embedded key go recursive
        if (f.getAnnotation(EmbeddedId.class) != null || f.getAnnotation(Id.class) != null) {
            isOwnField = true;
            pkm = new PrimaryKeyMetadata();
            pkm.setPartition(isPartitionKey);
            if (isPartitionKey) {
                pkmeta.setPartitionKey(pkm);
            } else {
                result.setPrimaryKeyMetadata(pkm);
            }
            parsePropertyLevelMetadata(f.getType(), result, pkm, true);
        }
        if ((f.getAnnotation(Transient.class) == null && javaTypeToDataType.get(f.getType()) != null) || isOwnField || f.getType().isEnum()) {
            Method getter = null;
            Method setter = null;
            for (Method m : methods) {
                // setter are defined
                if (isGetterFor(m, f.getName())) {
                    getter = m;
                } else if (isSetterFor(m, f)) {
                    setter = m;
                }
                if (setter != null && getter != null) {
                    String columnName = getColumnName(f);
                    DataType.Name dataType = getColumnDataType(f);
                    EntityFieldMetaData fd = new EntityFieldMetaData(f, dataType, getter, setter, columnName);
                    if (pkmeta != null && !isOwnField) {
                        fd.setPartition(pkmeta.isPartition());
                        fd.setPrimary(true);
                        pkmeta.addField(fd);
                    } else if (isOwnField) {
                        pkm.setOwnField(fd);
                    }
                    if (f.getAnnotation(EmbeddedId.class) != null) {
                        break;
                    }
                    if (f.getAnnotation(Version.class) != null) {
                        result.setVersionField(fd);
                    }
                    setCollections(f, fd);
                    if (f.getAnnotation(Static.class) != null) {
                        fd.setStatic(true);
                    }
                    if (f.getAnnotation(GeneratedValue.class) != null) {
                        fd.setAutoGenerate(true);
                    }
                    result.addField(fd);
                    // exit inner loop on filed's methods and go to
                    break;
                // the next field
                }
            }
        }
    }
    return result;
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) PrimaryKeyMetadata(com.datastax.driver.mapping.meta.PrimaryKeyMetadata) EmbeddedId(javax.persistence.EmbeddedId) Method(java.lang.reflect.Method) GeneratedValue(javax.persistence.GeneratedValue) Field(java.lang.reflect.Field) Version(javax.persistence.Version) DataType(com.datastax.driver.core.DataType) Transient(javax.persistence.Transient) Static(com.datastax.driver.mapping.annotation.Static)

Example 2 with PrimaryKeyMetadata

use of com.datastax.driver.mapping.meta.PrimaryKeyMetadata 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 3 with PrimaryKeyMetadata

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

the class EntityTypeParserTest method testGetEntityMetadataForSimpleCompoundKey.

@Test
public void testGetEntityMetadataForSimpleCompoundKey() {
    EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityWithKey.class);
    assertEquals("test_entity", meta.getTableName());
    assertEquals(0, meta.getIndexes().size());
    assertEquals(6, meta.getFields().size());
    PrimaryKeyMetadata pkm = meta.getPrimaryKeyMetadata();
    assertNotNull(pkm);
    assertTrue(pkm.isCompound());
    assertFalse(pkm.hasPartitionKey());
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) PrimaryKeyMetadata(com.datastax.driver.mapping.meta.PrimaryKeyMetadata) Test(org.junit.Test)

Example 4 with PrimaryKeyMetadata

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

the class EntityTypeParserTest method testGetEntityMetadataForSimplePojo.

@Test
public void testGetEntityMetadataForSimplePojo() {
    EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(Simple.class);
    assertEquals("Simple", meta.getTableName());
    assertEquals(0, meta.getIndexes().size());
    assertEquals(6, meta.getFields().size());
    PrimaryKeyMetadata pkm = meta.getPrimaryKeyMetadata();
    assertNotNull(pkm);
    assertFalse(pkm.isCompound());
    assertFalse(pkm.hasPartitionKey());
    assertEquals("id", pkm.getOwnField().getColumnName());
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) PrimaryKeyMetadata(com.datastax.driver.mapping.meta.PrimaryKeyMetadata) Test(org.junit.Test)

Example 5 with PrimaryKeyMetadata

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

the class EntityTypeParserTest method testGetEntityMetadataForCompoundKey.

@Test
public void testGetEntityMetadataForCompoundKey() {
    EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class);
    assertEquals("test_entity_composites", meta.getTableName());
    assertEquals(0, meta.getIndexes().size());
    assertEquals(8, meta.getFields().size());
    PrimaryKeyMetadata pkm = meta.getPrimaryKeyMetadata();
    assertNotNull(pkm);
    assertTrue(pkm.isCompound());
    assertTrue(pkm.hasPartitionKey());
    PrimaryKeyMetadata pk = pkm.getPartitionKey();
    assertTrue(pk.isCompound());
    assertFalse(pk.hasPartitionKey());
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) PrimaryKeyMetadata(com.datastax.driver.mapping.meta.PrimaryKeyMetadata) Test(org.junit.Test)

Aggregations

PrimaryKeyMetadata (com.datastax.driver.mapping.meta.PrimaryKeyMetadata)5 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)4 Test (org.junit.Test)3 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)2 DataType (com.datastax.driver.core.DataType)1 Static (com.datastax.driver.mapping.annotation.Static)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ExecutionException (java.util.concurrent.ExecutionException)1 EmbeddedId (javax.persistence.EmbeddedId)1 GeneratedValue (javax.persistence.GeneratedValue)1 Transient (javax.persistence.Transient)1 Version (javax.persistence.Version)1