use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class TableDefinitionTest method testForeignKeyField.
public void testForeignKeyField() {
TableDefinition def = new TableDefinition("foo");
ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
def.addField("product", fk);
assertEquals("CREATE TABLE IF NOT EXISTS `foo` (" + "`product` integer," + "FOREIGN KEY (`product`) " + "REFERENCES `product` (`mId`) " + "ON DELETE CASCADE);", def.toString());
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class ForeignKeyFieldTest method testSetAndGet.
public void testSetAndGet() {
Product p = new Product();
p.setName("test product");
ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
fk.set(p);
assertEquals(p.getName(), fk.get(getContext()).getName());
p.save(getContext());
fk = new ForeignKeyField<Product>(Product.class);
fk.set(p.getId());
assertEquals(p.getName(), fk.get(getContext()).getName());
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class DatabaseBuilder method getRelationDefinitions.
@SuppressWarnings("unchecked")
private static final <T extends Model> void getRelationDefinitions(T instance, Class<T> clazz, List<TableDefinition> definitions) {
if (clazz != null && clazz.isInstance(instance)) {
for (Field field : getFields(clazz, instance)) {
try {
Object o = field.get(instance);
if (o instanceof ManyToManyField) {
ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) o;
String leftHand = getTableName(clazz);
String rightHand = getTableName(m.getTarget());
TableDefinition definition = new TableDefinition(m.getRelationTableName());
ForeignKeyField<T> leftLink = m.getLeftLinkDescriptor();
ForeignKeyField<?> rightLink = m.getRightHandDescriptor();
definition.addField(leftHand, leftLink);
definition.addField(rightHand, rightLink);
definitions.add(definition);
}
} catch (IllegalAccessException e) {
Log.e(TAG, "could not gather relation definitions for class " + clazz.getSimpleName(), e);
}
}
getRelationDefinitions(instance, Model.getSuperclass(clazz), definitions);
}
}
Aggregations