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;
}
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;
}
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());
}
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());
}
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());
}
Aggregations